Magento add debug trace in system.log? - php

This error keeps filling system.log:
2016-03-27T13:59:10+00:00 ERR (3): Warning: simplexml_load_string(): Entity: line 12: parser error : Premature end of data in tag config line 2 in /home/xxxx/public_html/lib/Varien/Simplexml/Config.php on line 510
Every a few minutes or a few hours. To find the culprit, I tried to add this in Config.php:
file_put_contents('/home/xxxx/public_html/xml_errors/'.sha1(microtime(true)).'.txt', $string . PHP_EOL . '=====' . PHP_EOL . print_r(debug_backtrace(), true));
Before the 510 line of Config.php:
$xml = simplexml_load_string($string, $this->_elementClass);
Which is very quickly creating 5 - 10 files per second as my store is pretty busy and I still don't know how to pinpoint the $string that actually raises the warning.
How can I customize system.log to give debug trace of the warning so I can trace the actual source of the xml string that was passed to Config.php?
Or any other suggestion to find the source of the invalid XML would be very much appreciated!

I was able to trace back by adding to the problematic line:
$xml = simplexml_load_string($string, $this->_elementClass);
Like this:
set_error_handler("warning_handler", E_WARNING);
$xml = simplexml_load_string($string, $this->_elementClass);
restore_error_handler();
And then add a standalone function at the end of the Config.php file:
function warning_handler($errno, $errstr, $errfile, $errline) {
file_put_contents('/home/xxxx/public_html/xml_errors/'.sha1(microtime(true)).'.txt', print_r(debug_backtrace(), true));
}
You must manually create the folder xml_errors of course.

Related

Error while reading an XML and validating the schema with XMLReader in PHP

I'm having the following PHP code block:
if (file_exists($name)) {
$reader = new XMLReader();
$reader->open($name);
$reader->setSchema('inc/schema.xsd');
while($reader->read());
$reader->close();
} else {
die("you're having trouble with the files!");
}
For certain URLs I'm getting the following error:
Warning: XMLReader::read(): Unimplemented block at
..\xmlschemas.c:28351 in xml2csv.php on line
43
Warning: XMLReader::read(): An Error Occurred while reading in
xml2csv.php on line 43
Warning: DOMDocument::load(): Premature end of data in tag Products
line 1 in products.xml, line:
532571 in xml2csv.php on line 55
Mostly, the URLs that give me this problem are local URLs (when $name = "file.xml") or remote URLs (when $name = "http://www.domain.com/products.xml") are broken (returning 404, 500, etc.).
A simple Google search on the error brought me to this: https://github.com/Chronic-Dev/libxml2/blob/master/xmlschemas.c
At the line mentioned, 28351, it seems there's a text saying TODO. Because my C skills are very limited (as in close to nothing), I'd like to better understand the reasons of this error and how can I avoid bumping into it.
As XMLReader states, open method return boolean, which is TRUE in case of success and FALSE in case of failure. You may want to try the snippet below?
if (file_exists($name)) {
$reader = new XMLReader();
if ($reader->open($name)) {
$reader->setSchema('inc/schema.xsd');
while($reader->read());
$reader->close();
}
} else {
die("you're having trouble with the files!");
}

Clearing Cache In Magento Removes The Cart

I've inherited a Magento project and needed only to change the value of two config variables, both payment URLs in table core_config_data.
I did so easily, but the old values persisted when loading the payment button pages. I then attempted to clear the cache by running the following:
$mage_filename = 'app/Mage.php';
require_once $mage_filename;
umask(0);
Mage::run();
Mage::app()->cleanCache();
Now the link to the Shopping Cart is gone from the main UI. I also get 404s when attempting to add items to the cart. When displaying product pages I receive the following error message:
Fatal error: Call to a member function addExcludeProductFilter() on a non-object in /home/rcspaces/public_html/shop/app/code/core/Mage/Catalog/Block/Product/List/Upsell.php on line 61
Please offer any suggestions you may have to help recover. Thanks.
open all your log files in magento, apache, system log and exception log. Chances are one of your xml files is corrupted.
then, if you notice a error like this one:
2014-03-12T06:15:28+00:00 ERR (3): Warning: simplexml_load_string(): Entity: line 1: parser error : XML declaration allowed only at the
start of the document in /var/www/magento1/lib/Varien/Simplexml/Config.php on line 510
2014-03-12T06:15:28+00:00 ERR (3): Warning: simplexml_load_string(): in /var/www/magento1/lib/Varien/Simplexml/Config.php on line 510
2014-03-12T06:15:28+00:00 ERR (3): Warning: simplexml_load_string(): ^ in /var/www/magento1/lib/Varien/Simplexml/Config.php on line 510
open up /var/www/magento1/lib/Varien/Simplexml/Config.php, navigate to line 510 and modify loadString function to look lie this:
public function loadString($string)
{
if (is_string($string)) {
$xml = simplexml_load_string($string, $this->_elementClass);
if ($xml instanceof Varien_Simplexml_Element) {
$this->_xml = $xml;
return true;
} else {
Mage::log($string);
}
} else {
Mage::logException(new Exception('"$string" parameter for simplexml_load_string is not a string'));
}
return false;
}
then, clear cache (can use 'rm -rf /var/www/magento1/var/cache/' unix command) and check system log again. You should see the content of the broken xml file. fix it comparing to vanilla magento files and you should be good. my broken xml was /var/www/magento1/app/code/core/Mage/Checkout/etc/config.xml
when I reproduced the error here.
once this is working, make sure to remove the else with Mage::log($string) from the Config.php file you modified earlier.
It sounds like you're missing server rewrite rules (.htaccess if you're on apache).
What version of Magento are you on?

Premature end of script headers on index.php magento

I have searched this a lot and none of the solutions has worked so far, so I guess I really need to troubleshoot to see what the problem is:
When I browse our website www.theprinterdepo.com, it works fine on chrome, ie or firefox.
However when I go to the admin on www.theprinterdepo.com/admin on Internet explorer, then it shows the 500 internal server error, and after that if I try to use Internet Explorer to www.theprinterdepo.com, it shows the same error. At the same time I can be in Google Chrome or Firefox surfing the site and it works without a problem.
It takes ages to load on Internet Explorer when it works. while on chrome still loads perfect.
I set chmod 755 in index.php.
I added this on index.php
if ($_SERVER['REMOTE_ADDR'] == '83.134.93.212') { Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); }
I got this
Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 60: parser error : Opening and ending tag mismatch: config line 28 and confg in /home/theprint/public_html/lib/Varien/Simplexml/Config.php on line 510
#0 [internal function]: mageCoreErrorHandler(2, 'simplexml_load_...', '/home/theprint/...', 510, Array)
#1 /home/theprint/public_html/lib/Varien/Simplexml/Config.php(510): simplexml_load_string('loadString('loadFile('/home/theprint/...')
#4 /home/theprint/public_html/app/code/core/Mage/Admin/Model/Config.php(59): Mage_Core_Model_Config->loadModulesConfiguration('adminhtml.xml', Object(Varien_Simplexml_Config))
Line 510 correspond to this:
public function loadString($string)
{
if (is_string($string)) {
$xml = simplexml_load_string($string, $this->_elementClass);
if ($xml instanceof Varien_Simplexml_Element) {
$this->_xml = $xml;
return true;
}
} else {
Mage::logException(new Exception('"$string" parameter for simplexml_load_string is not a string'));
}
return false;
}
Since your error is Opening and ending tag mismatch: one of the quickest way to debug this is to disable all custom module/layout.xml modification and then reenable them one by one, until you find the xml that causing this error.
by editing that config file I was able to ech the path and find the xml failing, the start tag was and the end tag was
I fixed that and then it started to work

Is there a way to easily open/edit the files shown in the PHP errors in your browser?

Is there a way (through a browser addon or other means) to easily open the files shown to be causing errors?
For example, it would be nifty to be able to click on the Notice below and open the file in my IDE, rather than having to go back to my IDE and look for the file when the notice appears.
Notice: Undefined index: xyz in C:\xampp\htdocs\index.php on line 141
You could possibly do something using set_error_handler and register_shutdown_function (to handle fatal errors) to intercept the errors and display them differently. The example below isn't a fully functional of what you're asking but should point you in the right direction. I'll leave it up to you to figure out how to launch the program of your choice.
<?php
function error_handler($errno, $errstr, $errfile, $errline)
{
echo $errstr . ' in '.$errfile.' on line ' . $errline . PHP_EOL;
}
function shutdown_handler()
{
$err = error_get_last();
// Fatal errors don't trigger the error handler so trigger the error handler manually.
if ( ! empty($err['type']) && $err['type'] === E_ERROR )
error_handler($err['type'], $err['message'], $err['file'], $err['line']);
}
set_error_handler('error_handler');
register_shutdown_function('shutdown_handler');
// The following 2 line throw errors.
preg_replace('invalid regex', '', 'Sample');
stack('overflow');
After some further research I have found that installing Xdebug and making use of the file_link_format configuration variable was exactly what I needed.
This forum thread helped me to get it set up with Netbeans which is the IDE I am using but you will be able to get it to work with any IDE/text editor with a change to the batch file they suggest.

PHP error line numbers completely off

I don't know what's going on with my PHP. I'm using a bunch of classes and a semi-MVC framework that I developed myself (as an experiment). So the PHP file is including a lot of class files.
My PHP line numbers for error messages are completely off and therefore useless and making it impossible for me to debug. For example, right now I'm getting an error message that says:
`Parse error: syntax error, unexpected ')' in /view.php on line 209
The only problem is: there is no ')' anywhere near line 209. Even worse, if I put die() on line 200 or so, it still gives me an error message, now pushed down to line 210. So clearly the line of code is being taken into account, yet for some reason it's not dying.
Another thing is, it's including a header.php file prior to this line. The header file basically just outputs some HTML, and works fine on other pages. Yet on this page, it doesn't even output the header; it's just dying with a blank page and that error message.
Is there anything I can do to use more reliable debugging? If I could have an accurate line number, I'm sure I'd find the bug easily.
Edit: I found the bug. The point of this question is not to solve the unexpected ')' bug. The point is: why are the line numbers inaccurate? The actual error message was on line 218, not 209 or 210.
It's your line endings. Open up a program where you can change the line endings and change it to that of the server and the line number will be correct. I don't know about windows, but on mac you can change line endings using TextWrangler (or BBEdit) and on linux using gedit.
Try adding a Logging function with a simple stacktrace.
Use it:
<?php
require_once("Log.class.php");
$myLog = new Log();
$log->write("Tag here", "Details here");
?>
Log.class.php:
<?php
$debugFolder = "/";
class ExpectedParamaterUndefinedException extends Exception {}
class Log {
private $logs = array();
function __contruct(){
}
function write($type, $message){
$trace=debug_backtrace();
$caller=array_shift($trace);
$fileParts = explode("/", $caller['file']);
$filePieces = explode(".", $fileParts[count($fileParts) -1]);
$this->logs[ $filePieces [count($filePieces ) - 2] ] .= " [ ".date(DATE_RFC822)." ] [ $type ] [Line : ".$caller['line']."] ::: $message \n ";
}
function __destruct(){
foreach($this->logs as $caller => $log){
$fp=fopen($debugFolder . "debug/" . $caller.".log", "a");
fwrite($fp, $log);
fclose($fp);
}
}
}
?>
You will end up with a .log file for each class that you log.
Happy hunting!

Categories