How to register Zend_Log in the bootstrap of a ZF 1.8+ application?

Andrew picture Andrew · Nov 16, 2009 · Viewed 8.8k times · Source

So I want to start logging in my Zend Framework application. I want to register the logger somewhere in my bootstrap so that I can easily access it from any controller action. I think this should be a simple thing that has been done before, but how can I do this?

The documentation shows something like this, but I don't want to create a new Zend_Log every time I want to log something:

$writer = new Zend_Log_Writer_Stream('/path/to/my/log/file');
$logger = new Zend_Log($writer);
$logger->log('Informational message', Zend_Log::INFO);

Solution

This is what I came up with. Thanks for the reminder about Zend_Registry!

// in /application/Bootstrap.php
protected function _initLogger()
{
    $writer = new Zend_Log_Writer_Stream('php://output');
    $logger = new Zend_Log($writer);
    Zend_Registry::set('logger', $logger);
}

// in controller actions
$logger = Zend_Registry::get('logger');
$logger->log('message');

Answer

linead picture linead · Nov 16, 2009

The easiest is to use Zend_Registry to store the log

Use this inside your bootstrap

Zend_Registry::set('log', $log);

and use this to fetch that log

Zend_Registry::get('log')