I’m : a web and mobile developer based in the Manning Valley, Australia.

Zend_Log and custom writer Log_Writer_Script

Yesterday I wrote about my Ruby script which I was going to use for sending critical log information via Jabber. Well since I use the Zend Framework for almost all of my PHP work I decided to go about creating my own custom writer for Zend_Log. My first thoughts were to have a Zend_Log_Writer_Jabber class but in the end decided on something a bit more generic and so Log_Writer_Script was born.

    $params = array(
        'script'   => '/usr/bin/send_jabber.rb',
        'username' => 'my_username',
        'password' => 'my_password',
        'message'  => '"%LOG_ENTRY%"'
    $writer = new Log_Writer_Script( $params );
    $logger = new Zend_Log( $writer );
    $logger->log( 'The world is falling over!', Zend_Log::EMERG );

Run that and it will execute as follows:

    /usr/bin/send_jabber.rb --username=my_username --password=my_password --message="The world is falling over!"

Cool huh? And it's extremely generic so should be quite bendable to all your needs. The array of parameters can also accept a key of 'parameter_separator' which by default is set to ' --%s=%s'. This, along with sprintf, is used to generate the command line call. The %s in there are the key/value pairs from the array. Keys of 'script' and 'parameter_separator' are stripped out so won't end up being sent through to your script.

Here's the source if you'd like /php/zend/log/Log_Writer_Script.phps