Zend Framework 2 - How do you implement log service?

26 Jul 2013 · One minute read · on Gianluca's blog

News! Tech related notes are NOW published to ShippingBytes. See you there! I always felt this was not the right place for me to write consistently about tech and tools. So if you want to read more about that see you at the other side

A log system is an essential element for any application. It is a way to check the status and use of the application. For a basic implementation you can refer to the fig-stanrdars organization PSR-3 article, that describes th elogger interface.

Zend Framework 2 implement a Logger Component, the following is an example of how to use it with service manager.

<?php
return array(
	'service_manager' => array(
		'abstract_factories' => array(
			'Zend\Log\LoggerAbstractServiceFactory',
		),
	),
	'log' => array(
		'Log\App' => array(
			'writers' => array(
				array(
					'name' => 'stream',
					'priority' => 1000,
					'options' => array(
						'stream' => 'data/app.log',
					),
				),
			),
		),
	),
);

LoggerAbstractServiceFactory is a Service Factory, as an example, into service Manager class Logger and will be used in the whole application. Log/App is the name of a single logger, and writer is an adapter that is used to choose the method of writing, in this case everything is written to file, but you can use a DB adapter and write your log into database.

<?php
namespace GianArb\Controller;
class GeneralController
	extends AbastractActionController
{
	public function testAction(){
		$logger = $this->getServiceLocator()->get('Log\App');
		$logger->log(\Zend\Log\Logger::INFO, "This is a little log!");
	}
}

With this configuration Log\App writes a string into data/app.log file, with INFO property. By default you can use an array of properties.

<?php
protected $priorities = array(
	self::EMERG  => 'EMERG',
	self::ALERT  => 'ALERT',
	self::CRIT   => 'CRIT',
	self::ERR    => 'ERR',
	self::WARN   => 'WARN',
	self::NOTICE => 'NOTICE',
	self::INFO   => 'INFO',
self::DEBUG  => 'DEBUG',
);

Usage of different keys is a good practice because it is very easy to write filter or log categories.

Another good practice, valid for all services in general, is to create your class extending single service.

<?php
use Zend\Log\Logger
class MyLogger extends Logger

This choice helps managing future customizations of services and is another important layer for managing unexpected updates.

Rali, thanks for your help with my robotic english! :P

Something weird with this website? Let me know.