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
1 2 |
Mage::log('My log entry'); Mage::logException($e); |
Function Arguments
Take a look at the function signature of this function.
1 2 3 4 5 6 7 8 9 10 11 12 |
/** * log facility (??) * * @param string $message * @param integer $level * @param string $file * @param bool $forceLog */ public static function log($message, $level = null, $file = '', $forceLog = false) { ... } |
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:
1 2 3 4 5 6 7 8 |
const EMERG = 0; // Emergency: system is unusable const ALERT = 1; // Alert: action must be taken immediately const CRIT = 2; // Critical: critical conditions const ERR = 3; // Error: error conditions const WARN = 4; // Warning: warning conditions const NOTICE = 5; // Notice: normal but significant condition const INFO = 6; // Informational: informational messages const DEBUG = 7; // Debug: debug messages |
(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
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Mage::log('My Message'); // level is warning, you should use the code below and avoid magic number Mage::log('My Message', 4); // level is alert Mage::log('My Message', Zend_Log::ALERT); // log an array Mage::log($myArray); // log an object Mage::log($myObject); // level is warning, store to /var/log/debug.log Mage::log('My Message', null, 'debug.log'); // level is warning, store to /var/log/debug.log, force logging Mage::log('My Message', null, 'debug.log', true); // log an exception Mage::logException($e); |