This article is a continuation of Log Your PHP Application With Zend_Log (Part 2). Check it out if you missed it. Last time I wrote about installation and some features of Zend_Log. Now I will explain about Zend_Log usage.
Zend_Log Usage
To make log message, first time you must make logger, then make writer and then insert writer into logger. One thing that must be remembered, logger must contain at least one writer to write out log message.
Example:
<?php
require_once 'Zend/Log.php';
require_once 'Zend/Log/Writer/Stream.php';
// make logger object
$logger = new Zend_Log();
// make writer object to write log message to file application.log
$writer = new Zend_Log_Writer_Stream('application.log');
// insert writer to logger
$logger->addWriter($writer);
Writer can be inserted into logger when make logger object.
<?php
$writer = new Zend_Log_Writer_Stream('application.log');
$logger = new Zend_Log($writer);
To write log message into file application.log, can do with call log() method on logger. With define level priority of log message on second argument.
<?php
$logger->log('Just message', Zend_Log::INFO);
Then we see contain of application.log
$ cat application.log
2009-03-29T12:28:01+07:00 INFO (6): Just message
Logger has magic method that allow us to make log message based on priority with call the priority as a method.
Example:
<?php
$logger->log('Info message', Zend_Log::INFO);
$logger->log('Error message', Zend_Log::ERR);
// can do it with this way
$logger->info('Info message');
$logger->err('Error mesage');
From code above, we can write log message to a file.
Formatter usage, filter and priority on Zend_Log
As I mentioned before log message that resulted from Zend_Log consist of some events. And default format from the event is:
'%timestamp% %priorityName% (%priority%): %message%'
Work on MVC pattern with many controller make us need to know where log message come from. Default format of log message is not suffice our need. I will give an example to make my own log message format with extend Zend_Log. And in the code below, I will separate error log message with another log message with filter object
<?php
require_once 'Zend/Log/Formatter/Simple.php';
require_once 'Zend/Log/Filter/Priority.php';
require_once 'Zend/Log/Writer/Stream.php';
require_once 'Zend/Log.php';
class DollyAswin_Log extends Zend_Log
{
public function __construct()
{
// set formatter, add %class% to save class name
$format = '%timestamp% (%priorityName%) %priority% %class%: %message%'
. PHP_EOL;
$this->_formatter = new Zend_Log_Formatter_Simple($format);
parent::addWriter($this->_errorWriter());
parent::addWriter($this->_allWriter());
parent::__construct();
}
/**
* Writer for error log message
* Error log message will write to file error.log
*/
protected function _errorWriter()
{
$writer = new Zend_Log_Writer_Stream('error.log');
$writer->addFilter(new Zend_Log_Filter_Priority(Zend_Log::ERR));
$writer->setFormatter($this->_formatter);
return $writer;
}
/**
* Writer for another log message
* Another log message will write to file system.log
*/
protected function _allWriter()
{
$writer = new Zend_Log_Writer_Stream('system.log');
$writer->setFormatter($this->_formatter);
return $writer;
}
}
Save code above as Log.php in folder DollyAswin. Class above will be included on every controller. Here is an example to make logger object from the class above.
Example:
require_once 'DollyAswin/Log.php';
class TesController
{
public function init()
{
// make instantiate from Tes model
$this->_tes = new Tes;
// make logger object from DollyAswin_Log
$this->_logger = new DollyAswin_Log();
// set event class with name of this class
$this->_logger->setEventItem('class', __CLASS__);
}
public function add()
{
try {
$lastId = $this->_tes->insert($data);
$this->_logger->info('Insert ID ' . $lastId);
}
catch (Zend_Db_Exception $e) {
$this->_logger->err($e->getMessage());
}
}
}
If add() method from code above success run, it will result log message like this on file system.log
$ cat system.log
2009-03-29T12:28:01+07:00 INFO (6) TesController: Insert ID 1
If fail,it will result log on file error.log that contain error log message from that application.
DollyAswin_Log usage is same with Zend_Log, because that class is extend from Zend_Log. On that class, writer, filter and formatter has been set. So you don't need to set them on every controller. Just make object from DollyAswin_Log.
We have know the way to make log with Zend_Log. So, this series article has finish. You can expand the way to make log with Zend_Log, like write log message to database, you can do it with Zend_Log_Writer_Db (this class have deppedencies to Zend_Db).
Comments
Post new comment