Magento's Mage::log() causes white screen - php

When using Magentos log facility Mage::log() it sometimes causes a white screen. No error messages are outputted either to the screen or any of the log files (var/log/system.log, var/log/exception.log )
This seems to happen only when I'm trying to log a very large object. for example when I'm trying this
Mage::log(Mage::helper('catalog/category')->getStoreCategories());
inside a block controller it causes a white screen.
the same happens when I'm trying to log the current product in app/design/frontend/enterprise/default/template/catalog/product/view/media.phtml
using
Mage::log($_product);
Usually Mage::log() works fine and writes everything to the system.log file.
I was wondering if this has happened to anybody else or if anybody has an idea about why this is happening?

Mage::log works a lot like print_r, private and protected values are printed too which includes extensive resource details. You can avoid these by using the purpose made Varien_Object::debug method.
Mage::log($_product->debug());
debug is also preferred because it detects recursion which not all versions of PHP do.

I guess it happens to everyone.
Try not to log large objects.
In case you need I would advice you to use die.
for example like this
$object = ...;
die($object);
or
die('pre html tag'.print_r($object,true).'pre html tag');

Related

Why is this use of count() not valid in php?

There are a bunch of questions relating to errors with the count() logic in some phpmyadmin libraries but they all seem slightly different to this one.
I'm at a bit of a loss with this strange behavior in the 'Designer' tab. I'm informed that 'errors have been detected on the server!' and that I should 'look at the bottom of this window', but as you can see from the image there is nothing in the console and the error message is obscured:
After getting this popup every time I opened the Designer tab, I decided to track it down:
... and looking at the line in question, #405, in /usr/share/phpmyadmin/libraries/pmd_common.php, I found:
if (count($min_page_no[0])) {
... but isn't this a perfectly legal use of the count() function?
This is in a library file so how can I debug this? - I tried to var_dump($min_page_no[0]); and reload the page to see what I'm dealing with, but nothing was displayed.
Update your installation of phpMyAdmin.
I believe 4.7.8 addresses this, as evidenced by the release tag on this commit:
https://github.com/phpmyadmin/phpmyadmin/commit/c77cfa7d13370a7f1e3236c5896f89981e61406f
[Edit: And an explanation of why count isn't valid in this case: That particular index isn't always set. If they try to use count() on an array index that isn't set, it'll throw that warning.]

PHP eval code and store the result into a variable

I have continued my voyage into creating a extremely simple template engine.
Because I wanted to add logic to my template I eventually got back to the point that I allowed PHP tags into my code which I enabled by evalling the code.
Maybe not the best solution but when looking at the WordPress templates I noticed that the idea itself may not be that bad.
But now there still is one small problem left.
And that is that I want to translate the generated code.
But it has been evalled already. Hence parsed.
I thought of solving this problem by using ob_get_contents().
But this brought one more question and in case of errors it shows a white screen. (memory usage etc.)
Plus it still did not take away the problem of eval that it parsed the contents when evalled.
In short the class logic is:
Loading template files
Adding the contents
Compiling the template
Eval the code (but unfortunately also displaying the code)
Translate the code so I can translate the code parsed by a PHP script
I would love something like:
$code = eval('?>'.$tpl.'<?php');
$code = translate($code);
WriteCache($code);
SetDocumentHeader();
echo $code;
Would anyone know how to achieve this?
Thanks in advance!
$code = eval($tpl);
Check this out.

How can I find out the value of a variable while writing PHP code (for debugging)?

I'm writing in symfony and I'm in one of the actions.class.php files.
Normally I would just echo the variable's value but because this is an actions class module, I can't do that because I can't output to the page.
I was thinking of using FirePHP but it involves installing it server-side and I'd like to be aware of an easier or built-in solution.
Emailing logs works for sure but there are some better, easier and faster ways to do that ;)
In symfony you have access to the Logger which will allow you to log anything. In prod environment by default it is disabled (of course you can switch it on) but in dev it's not only available in the log files but also prints in the Sf Web debug bar.
Inside an action use:
$this->getLogger()->debug('My $variable value is now: '.$variable);
You can also use other levels of logging (->warning(''), ->err(''), ->crit(''), etc.). Of course you need to change the variable to string if it is an array or an object.
In fact you can echo and var_dump things while in the action. They will show in your page (though mess badly with the layout as you're displaying stuff before the actual template).
Remeber that echo works only with strings and numbers. If you try to echo a boolean or array you will see nothing. If you try echo an object it's __toString() method will be used or an error raised.
Email yourself throughout the code. You could either send one at the end of the script or throughout. There are benefits to both. Emailing throughout will easily let you know where something went wrong if you don't get an email and emailing at the end save overhead(doesn't seem like it would be a problem here though).
If you have access to the server's log files, you might want to look into PHP's error_log function. It will print a string into PHP's error log and then you can check the log file.
-edit-: error_log also allows you to email yourself the message. But that's silly.

How to include GET & POST data in PHP error log

Is there a way to have PHP log errors to a file or email me errors INCLUDING $_POST[] & $_GET[] and $_SERVER[] data?
Right now I get a log of PHP FATAL and WARNING errors and 404 NOT_FOUND errors but it's hard to debug some errors without knowing things like user input and the referrer.
Thanks
error_log(print_r($_POST, true));
error_log(print_r($_GET, true));
Put that into a custom error handler and it'll log both for you (the 'true' parameter makes print_r return text instead of outputting it).
You might need to boost the max line length in the error log with log_errors_max_len, as it defaults to 1024 chars and will truncate everything after that (it won't split >1024 char data across multiple lines).
If you are using PHP 5 create a custom exception class which will extend the native PHP Exception class and add whatever data collecting / logging methods you like. That way you could easily combine try {} catch() {} block with Exceptions and have your own way of controlling how you want to log all data.
I would recommend the error_log() function. I use that a lot to debug stuff in php.
http://php.net/manual/en/function.error-log.php
I worked on a telecom project that used CI to handle a Felx based frontend, we hijacked all possible error reporting and wrote a wrapper on syslog functions and used it in our application, the result was we could log whatever data we wanted and the log was visible through system log viewer :-)

So Echo isn't echoing

So I've got all of this really neato PHP code and I've started doing some reuse with functions out of necessity. I'm debugging, trying to figure out why I can't delete comments on my website while I'm deleting folder (because who wants orphaned comments?)
So I have a call to deletefolder( $parent) inside a file called deletefolder.php. This a function that will recursively traverse my tree structure.
I've include another file inside deletefolder.php. The file is call helpers.php, and it contains the deletefolder function.
The deletefolder function calls deletecomments (kills all the comments per file) and delete file (which kills the file itself).
Now, all of it is just slathered with echo statements to help me figure out what's going on. When I call this combination of functions from other locations I don't seem to have a problem getting messages. But when I call them from the deletefolder.php page I don't get any. Does anybody know why this would be the case?
A few things you might want to verify.
Check the source of the output. You might be echoing straight in a middle of a HTML comment or a tag which is hiding the output.
Are you using output buffering (ob_start()) ? You might be clearing the buffer at some point in your code and forgot all about it.
Different files with the same name but not in the same directory. Do a die() in your function to make sure it actually reaches your code. You might be editing/including a copy of your file (happened to me quite a few times).
Well, I seriously doubt you've found a bug in the echo command, so the problem is with your program logic somewhere. Without seeing your code, it's impossible to say really. Perhaps there's some variable being set or unset unexpectedly, or you're not actually include()ing the files properly.

Categories