How to register a Zend\Log Instance in the ServiceManager in ZF2

Viewed 11257

I am wondering what is the best way to initiate and re-use a logger instance through the ServiceManager in ZF2. Of course I can do a simple method to be used in any class, like:

public function getLogger () {
        $this->logger = new Logger();
        $this->logger->addWriter(new Writer\Stream('/log/cms_errors.log'));
        return $logger;
    }

but I was wondering what is the best way to register a similar structure in the global.php. So far I can

add the following to global.php

'Zend\Log'=>array(
        'timestampFormat' => 'Y-m-d',
        array(
            'writerName'   => 'Stream',
            'writerParams' => array(
                'stream'   => '/log/zend.log',
            ),
            'formatterName' => 'Simple',            
        ),
    ),

If I try invoking it through:

$this->getServiceLocator()->get('Zend\Log')

I get a :

Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for Zend\Log
5 Answers
Related