Create your own Magento log files with Mage::log()

Magento comes with a built-in logging facility which can be enabled and configured in the back-end under System > Configuration > Developer. Alternatively you can enable the Developer Mode for the site to enable the log (on how to do this have a look at the index.php file). The exception log is always on.

Mage::log('My log entry');
Mage::log('My log message: '.$myVariable);
Mage::log($myArray);
Mage::log($myObject);
Mage::logException($e);

By default all log entries are appended to the var/log/system.log file and the exceptions are logged to var/log/exception.log. Objects and Arrays are automatically written via a print_r() directive. Watch out when using objects since these can get substantial in size. If you are in Developer Mode you can try if the object supports debug output via $object->debug().

Tip:
When using Mage::log($myVariable);
If $myVariable is empty you won’t get an entry in the log. Add a comment before and at least you will know that the variable at that stage is empty:

Mage::log(‘My variable: ‘.$myVariable);
If you are distributing your own extensions it helps a lot if you don’t have to sift through heaps of unrelated log entries to identify an issue. The solution is to log to your own files.

Looking at the method definition in app/code/Mage.php you will find

public static function log($message, $level = null, $file = ”) {}
so logging to your own file becomes as easy as

Mage::log(‘My log entry’, null, ‘mylogfile.log’);
and all our log entries are in their own file at var/log/mylogfile.log. Exceptions can only be logged to one file

Facebooktwittergoogle_plusredditpinterestlinkedinmail

Leave a Reply

Your email address will not be published. Required fields are marked *