Magento generally logs events to either system.log or exception.log, but what if you want to do some additional action when those logs are written to?
Not at all difficult.
Tracing mageno's log writing functionality, you will find this gem in the log handler located in app/Mage.php
$writerModel = (string)self::getConfig()->getNode('global/log/core/writer_model');
Normally this will point magento to 'Zend_Log_Writer_Stream'
This class has a method called _write($event) which handles the write to the file(s)
It is thus possible to define your own writer model, and intercept the core functionality of writing to the logs.
This then enables you to do some other actions before or after the log is written.
Here is how:
Firstly, in your module config.xml, inside the global tags, add the following configuration directive, and point it to your own model.
<log>
<core>
<writer_model>ProxiBlue_NewRelic_Model_Log_Writer_Stream</writer_model>
</core>
</log>
This little snippet of xml will override the default config (defined in Mage/Core/Etc/Config.xml) and point the magento log writer to your own class. In this example I pointed it to my newrelic module's class located in app/code/community/ProxiBlue/NewRelic/Model/Log/Writer/Stream
My custom class thus overrides the _write method, and after I call the parent method, I can do stuff with the $event data.
In this example, I push error notices off to NewRelic.
/**
* Intercept logs to new relic
*
* @category ProxiBlue
* @package ProxiBlue_NewRelic
* @author Lucas van Staden (support@proxiblue.com.au)
**/
class ProxiBlue_Newrelic_Model_Log_Writer_Stream extends Zend_Log_Writer_Stream {
/**
* Write a message to the log.
*
* @param array $event event data
* @return void
*/
protected function _write($event) {
parent::_write($event);
/// DO SOME STUFF WITH THE $event DATA, AFTER THE LOG WAS WRITTEN TO DISK
}