Magento Mage::log Function, Log Level, and Log Location Explained

Spread the love

Magento provides built-in API for logging. When you are developing new modules or debugging an installed module, these APIs could come in handy.

Mage::log Static Function

If you have been touching with Magento backend code, you have probably seen Mage::log() used by developers in their code. See the examples below, you can use this method to log strings, objects, arrays, etc. This static function is defined in Mage.php

Function Arguments

Take a look at the function signature of this function.

It takes 4 arguments:

The log (1) message, you can pass a string, an object, or an array. If it is an object or an array, it will call print_r function to print in a human-readable format.

The log (2) level, which is defined in lib\Zend\log.php. The default value is null,T and the function will automatically use DEBUG when it is null. In addition, you can also set log level as EMER, ALERT, CRIT, ERR, WARN, NOTICE, INFO, DEBUG.  Check out all possible log level below:

(3) File, if you don’t assign a custom log file location,  it will use default location to store log file. Mage::log function will log into /var/log/system.log; Mage::logException() will save exception printing into /var/log/exception.log

Additionally, you can change the default log locations in Magento back-end under

System -> Configuration -> Developer

Magento Enable Log

Note: see the configuration screen above. In order to store your log messages successfully. Remember if your Magento site is NOT in Developer Mode, and you set  ‘Enable’ to NO in the back-end, you will need to set the (4) forceLog to true , which is the forth argument taken by Mage:log function

However, if you are in Developer Mode, your log message should be stored successfully even if you set you set  ‘Enable’ to NO in back-end.

Mage::logException Static Function

logException function is just a wrapper of Mage::log, where level is set to Zend_Log::ERR. And file is set to default exception log location.

Usages

Last, let’s see some common usage of Mage::log function.

 

Leave a Reply

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