How to Create Custom Log File with Mage::log() in Magento

Magento comes with default logging system that can be extremely helpful when debugging any issues you may have. By default in Magento, the logging is not enabled, so we have to enable the logging in Magento admin side under System > Configuration > Developer (under ADVANCED tab) > Log Settings. Here we have to change the value of Enabled to YES. Once you enable, you can find them in /var/log folder within your Magento installation root.

Types of Magento Log:

Magento comes with two built-in log files, system.log & exception.log.

By default, all log entries (including warning, debug and errors messages from both native and custom modules) are appended to the system.log file and the exceptions are logged to exception.log.

Code to Add Log:

Use the below code to create a log message,

Mage::log('My log entry'); // Static text
Mage::log('My log message: ' . $myVariable); // Static text with dynamic value 
Mage::log($myArray); // Array value
Mage::log($myObject); // Object value
Mage::logException($e); // Exception value

Sometimes the system.log file has a lot of content, that time, it’s very difficult to find our log entries. To overcome this problem, we can create a separate custom log file for our module, with Magento it’s so easy:

Mage::log('My log entry', null, 'mylogfile.log'); // Static text
Mage::log('My log entry', Zend_Log::DEBUG, 'mylogfile.log'); // Static text with log type
Mage::log('My Variable: '.$myVar, null, 'mylogfile.log'); //pass a variable

In the above code,

  • The first parameter is our message.
  • The second parameter is specified the log type.
    Different Log Types:

    • Zend_Log::EMERG => Emergency: system is unusable
    • Zend_Log::ALERT => Alert: action must be taken immediately
    • Zend_Log::CRIT => Critical: critical conditions
    • Zend_Log::ERR => Error: error conditions
    • Zend_Log::WARN => Warning: warning conditions
    • Zend_Log::NOTICE => Notice: normal but significant condition
    • Zend_Log::INFO => Informational: informational messages
    • Zend_Log::DEBUG => Emergency: system is unusable
  • Third parameter is a filename. The file mylogfile.log will be added to the folder /var/log/

Note: Don’t forget to set full access permissions (777) to the following folder: /var/log

Hope this helps.

Leave a Reply

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