views:

266

answers:

2

Our Zend_Log is initialized by only adding the following lines to application.ini

resources.log.stream.writerName = "Stream"
resources.log.stream.writerParams.mode = "a"

So Zend_Application_Resource_Log will create the instance for us.

We are already able to access this instance in controllers via the following:

public function getLog()
{
    $bootstrap = $this->getInvokeArg('bootstrap');

    //if (is_null($bootstrap)) return false;

    if (!$bootstrap->hasPluginResource('Log')) {
        return false;
    }
    $log = $bootstrap->getResource('Log');
    return $log;
}   

So far, so good.

Now we want to use the same log instance in model classes, where we can not access the bootstrap.

Our first idea was to register the very same Log instance in Zend_Registry to be able to use Zend_Registry::get('Zend_Log') everywhere we want:

in our Bootstrap class:

protected function _initLog() {      
    if (!$this->hasPluginResource('Log')) {
        throw new Zend_Exception('Log not enabled');
    }

$log = $this->getResource('Log');

    assert( $log != null);

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

Unfortunately this assertion fails ==> $log IS NULL --- but why??

It is clear that we could just initialize the Zend_Log manually during bootstrapping without using the automatism of Zend_Application_Resource_Log, so this kind of answers will not be accepted.

+1  A: 

Possible it is not bootstraped at this time, try:

    try {
            $this->bootstrap('log'); // bootstrap log
            $logger = $this->getResource('log');
        } catch (Zend_Application_Bootstrap_Exception $e) {
            $logger = new Zend_Log();
            $writer = new Zend_Log_Writer_Null();
            $logger->addWriter($writer);
        }
        $registry = Zend_Registry::set('logger', $logger);
ArneRie
Can you explain how that works? I believe I will just end ob with the instance created in the catch block (with a Null Writer) attached to it.
Alex
it takes care the the log resource is bootstrapped thats the difference to your example.
ArneRie
But why do we need the catch block? As mentioned I end up with that logger that is created in the catch block and has a Writer_Null. So this $this->bootstrap('log') does not work. There is some "circular reference" exception. It looks, that $this->bootstrap('log') simply calls my protected function _initLog() again??
Alex
ahh.. i think the problem is the name of your method try to rename it to logger, because the zend resource is called log.
ArneRie
+3  A: 

This is the final solution. We basically shall not call the function _initLog()

Big thanks to ArneRie!

// Do not call this function _initLog() ! 
protected function _initRegisterLogger() {
    $this->bootstrap('Log');

    if (!$this->hasPluginResource('Log')) {
        throw new Zend_Exception('Log not enabled in config.ini');
    }

    $logger = $this->getResource('Log');
    assert($logger != null);
    Zend_Registry::set('Zend_Log', $logger);
}
Alex
That's because calling it `initLog` overrides the resource plugin. Which makes sense but isn't as apparent as it should be.
Htbaa
Yes, I know now, thanks for clarifying this.
Alex