A Writer is an object that inherits from Zend_Log_Writer_Abstract
. A Writer's
responsibility is to record log data to a storage backend.
Zend_Log_Writer_Stream
sends log
data to a PHP stream.
To write log data to the PHP output buffer, use the URL php://output
. Alternatively,
you can may like to send log data directly to a stream like STDERR
(php://stderr
).
<?php $writer = new Zend_Log_Writer_Stream('php://output'); $logger = new Zend_Log($writer); $logger->info('Informational message');
To write data to a file, use one of the Filesystem URLs:
<?php $writer = new Zend_Log_Writer_Stream('/path/to/logfile'); $logger = new Zend_Log($writer); $logger->info('Informational message');
By default, the stream opens in the append mode ("a"
).
To open it with a different mode, the Zend_Log_Writer_Stream constructor
accepts an optional second parameter for the mode.
The constructor of Zend_Log_Writer_Stream
also accepts an existing stream resource:
<?php $stream = @fopen('/path/to/logfile', 'a', false); if (! $stream) { throw new Exception('Failed to open stream'); } $writer = new Zend_Log_Writer_Stream($stream); $logger = new Zend_Log($writer); $logger->info('Informational message');
You cannot specify the mode for existing stream resources. Doing so
causes a Zend_Log_Exception
to be thrown.
Zend_Log_Writer_Db
writes log information to a database table using
Zend_Db
. The constructor of Zend_Log_Writer_Db
receives
a Zend_Db_Adapter
instance, a table name, and a mapping of database
columns to event data items:
<?php $params = array ('host' => '127.0.0.1', 'username' => 'malory', 'password' => '******', 'dbname' => 'camelot'); $db = Zend_Db::factory('PDO_MYSQL', $params); $columnMapping = array('lvl' => 'priority', 'msg' => 'message'); $writer = new Zend_Log_Writer_Db($db, 'log_table_name', $columnMapping); $logger = new Zend_Log($writer); $logger->info('Informational message');
The example above writes a single row of log data to the database table named
log_table_name
table. The database column named lvl
receives the priority number and the column named msg
receives the
log message.
The Zend_Log_Writer_Null
is a stub that does not write log data to anything.
It is useful for disabling logging or stubbing out logging during tests:
<?php $writer = new Zend_Log_Writer_Null; $logger = new Zend_Log($writer); // goes nowhere $logger->info('Informational message');
The Zend_Log_Writer_Mock
is a very simple writer that records
the raw data it receives in an array exposed as a public property.
<?php $mock = new Zend_Log_Writer_Mock; $logger = new Zend_Log($mock); $logger->info('Informational message'); var_dump($mock->events[0]); // Array // ( // [timestamp] => 2007-04-06T07:16:37-07:00 // [message] => Informational message // [priority] => 6 // [priorityName] => INFO // )
To clear the events logged by the mock, simply set $mock->events = array()
.
There is no composite Writer object. However, a Log instance can write
to any number of Writers. To do this, use the addWriter()
method:
<?php $writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile'); $writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile'); $logger = new Zend_Log(); $logger->addWriter($writer1); $logger->addWriter($writer2); // goes to both writers $logger->info('Informational message');