Writers
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.
Writing to Streams
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 send log data directly to a
stream like STDERR (php://stderr).
info('Informational message');
]]>
To write data to a file, use one of the
Filesystem
URLs:
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:
info('Informational message');
]]>
You cannot specify the mode for existing stream resources. Doing so
causes a Zend_Log_Exception to be thrown.
Writing to Databases
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:
'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.
Stubbing Out the Writer
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:
info('Informational message');
]]>
Testing with the Mock
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.
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().
Compositing Writers
There is no composite Writer object. However, a Log instance can write
to any number of Writers. To do this, use the addWriter()
method:
addWriter($writer1);
$logger->addWriter($writer2);
// goes to both writers
$logger->info('Informational message');
]]>