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
you can may like to send log data directly to a stream like
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_Exceptionto be thrown.
Zend_Log_Writer_Db writes log information to a database table using
Zend_Db. The constructor of
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_nametable. The database column named
lvlreceives the priority number and the column named
msgreceives the log message.
All data is sent via the
which uses HTTP headers to ensure the page content is not disturbed.
Debugging AJAX requests that require clean JSON and XML responses is possible with this approach.
Firefox Browser ideally version 3 but version 2 is also supported.
Firebug Firefox Extension which you can download from » https://addons.mozilla.org/en-US/firefox/addon/1843.
FirePHP Firefox Extension which you can download from » https://addons.mozilla.org/en-US/firefox/addon/6149.
Example #1 Logging with
/** * Place this in your bootstrap file */ $writer = new Zend_Log_Writer_Firebug(); $logger = new Zend_Log($writer); /** * Use this in your model, view and controller files */ $logger->log('This is a log message!', Zend_Log::INFO);
Example #2 Logging without
$writer = new Zend_Log_Writer_Firebug(); $logger = new Zend_Log($writer); $request = new Zend_Controller_Request_Http(); $response = new Zend_Controller_Response_Http(); $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance(); $channel->setRequest($request); $channel->setResponse($response); /** * Now you can make calls to the logger */ $logger->log('This is a log message!', Zend_Log::INFO); /** * Flush log data to browser */ $channel->flush(); $response->sendHeaders();
Built-in and user-defined priorities can be styled with the
$logger->addPriority('FOO', 8); $writer->setPriorityStyle(8, 'TRACE'); $logger->foo('Foo Message');
The default style for user-defined priorities can be set with the
The supported styles are as follows:
||Displays a plain log message|
||Displays an info log message|
||Displays a warning log message|
||Displays an error log message that increments Firebug's error count|
||Displays a log message with an expandable stack trace|
||Displays an error long message with an expandable stack trace|
||Displays a log message with an expandable table|
While any PHP variable can be logged with the built-in priorities, some special formatting is required if using some of the more specialized log styles.
TRACE styles require no special formatting.
To log a
Zend_Exception simply pass the exception object to the logger.
It does not matter which priority or style you have set as the exception is automatically
$exception = new Zend_Exception('Test exception'); $logger->err($exception);
You can also log data and format it in a table style. Columns are automatically recognized and the first row of data automatically becomes the header.
$writer->setPriorityStyle(8, 'TABLE'); $logger->addPriority('TABLE', 8); $table = array('Summary line for the table', array( array('Column 1', 'Column 2'), array('Row 1 c 1',' Row 1 c 2'), array('Row 2 c 1',' Row 2 c 2') ) ); $logger->table($table);
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:
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); // 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($writer1); $logger->addWriter($writer2); // goes to both writers $logger->info('Informational message');