Programmatically: How to create caustom Magento logging
Step 1: Create a custom log file
In your Magento module's etc/config.xml file, add the following code:
logger
Mage_Core_Model_Log_Abstract
custom.log
0520
This code defines a custom logger called custom_logger that will write to a file named custom.log.
Step 2: Create a custom log logger
In your Magento module's Model directory, create a new PHP file (e.g. Logger.php) and add the following code:
class YourModule_Model_Logger extends Zend_Log {
protected $_logger;
public function __construct($adapter = null) {
parent::__construct($adapter);
$this->_logger = Mage::getLog('custom_logger');
}
public function log($message, $level = Zend_Log::INFO) {
$this->_logger->log($message, $level);
}
}
This code extends the Zend_Log class and sets up the custom logger.
Step 3: Use the custom logger in your code
In your Magento module's code, you can now use the custom logger to log messages. For example:
$log = Mage::getModel('yourmodule/logger');
$log->log('This is a custom log message', Zend_Log::INFO);
This will write the message "This is a custom log message" to the custom.log file with an info level.
Full code example
Here is the full code example:
app/code/local/YourModule/etc/config.xml
logger
Mage_Core_Model_Log_Abstract
custom.log
0520
app/code/local/YourModule/Model/Logger.php
class YourModule_Model_Logger extends Zend_Log {
protected $_logger;
public function __construct($adapter = null) {
parent::__construct($adapter);
$this->_logger = Mage::getLog('custom_logger');
}
public function log($message, $level = Zend_Log::INFO) {
$this->_logger->log($message, $level);
}
}
app/code/local/YourModule/controllers/IndexController.php
class YourModule_IndexController extends Mage_Core_Controller_Varien_Action {
public function indexAction() {
$log = Mage::getModel('yourmodule/logger');
$log->log('This is a custom log message', Zend_Log::INFO);
// ... rest of your code ...
}
}