Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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');
like image 330
Andrew Avatar asked Nov 15 '09 23:11

Andrew


1 Answers

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')
like image 106
linead Avatar answered Sep 22 '22 14:09

linead