Programmer's Reference Guide
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).
$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:
$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:
$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:
$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.
Zend_Log_Writer_Firebug sends log
data to the Firebug
Console.
All data is sent via the Zend_Wildfire_Channel_HttpHeaders component
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.
Requirements:
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 28.1. Logging with Zend_Controller_Front
// Place this in your bootstrap file before dispatching your front controller
$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 28.2. Logging without Zend_Controller_Front
$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);
// Start output buffering
ob_start();
// 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 setPriorityStyle() method.
$logger->addPriority('FOO', 8);
$writer->setPriorityStyle(8, 'TRACE');
$logger->foo('Foo Message');
The default style for user-defined priorities can be set with the setDefaultPriorityStyle() method.
$writer->setDefaultPriorityStyle('TRACE');
The supported styles are as follows:
Table 28.1. Firebug Logging Styles
| Style | Description |
|---|---|
LOG |
Displays a plain log message |
INFO |
Displays an info log message |
WARN |
Displays a warning log message |
ERROR |
Displays an error log message that increments Firebug's error count |
TRACE |
Displays a log message with an expandable stack trace |
EXCEPTION |
Displays an error long message with an expandable stack trace |
TABLE |
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.
The LOG, INFO, WARN, ERROR
and 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
recognized.
$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);
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:
$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.
$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:
$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');
Search the Manual
Components
Languages Available
Translation Status Reports
View the current status report of Zend Framework manual translations.
