Magento create your own log files


Hi Friends,


Default Log files:

When developing your own module in Magento, it’s useful to pass variables and other messages out to a debug file so you can see how your module is working with its data. Magento comes with 2 built in log files, system.log & exception.log, both located in the var/log folder. To activate them, you must go to the Magento admin panel and under: System->Configuration->Developer->Log Settings, you will see a setting for enabling the logs.


system.log is used for general debugging and catches almost all log entries from Magento, including warning, debug and errors messages from both native and custom modules.

exception.log is reserved for exceptions only, for example when you are using try-catch statement.
To output to either the default system.log or the exception.log see the following code examples:

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

Custom Log Files:

Going through the system.log can be a nightmare, with dozens of other modules outputting to the log it can be like finding the proverbial, needle in a haystack. So why not create a custom log for your module!, With Magento its so easy:

Mage::log('My log entry', null, 'mylogfilename.log');
//pass a variable
Mage::log('My Variable: '.$myVar, null, 'mylogfilename.log');

And that’s all there is to it. You can pass variable, arrays, messages to yourself, whatever you want. Just be careful when outputting native Magento objects, as they can be really really big. (Don’t ever try to output and entire product object!)

Custom Log Files with current year month and date:

If you want to save log file in your own separate folder with date here is code.

//Get today date
$todatedate = date('Y-m-d');

//Check log folder is created or not
$io = new Varien_Io_File();
$io->checkAndCreateFolder(Mage::getBaseDir('var').DS.'log'.DS.'YourfolderName'.DS.date('Y').DS.date('m'));

$logfilename = 'YourfolderName' . DS . date('Y') . DS . date('m') . DS . $todatedate . '_yourlogfilename.log';
Mage::log('testing log', null, $logfilename, true);

This file is store this path.
YourMagentoRootDirectory/var/log/YourfolderName/CurrentYear/CurrentMonth/Currentdate_yourlogfilename.log
EX. Magento/var/log/test/2014/05/2014-05-06_testing.log

Now Flush Magento Cache and refresh page

Hope this information is helpful to you :)

0 comments: