PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Writing to Firebug

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:

Example 588. Logging with Zend_Controller_Front

<?php
// 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 589. Logging without Zend_Controller_Front

<?php
$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();

Setting Styles for Priorities

Built-in and user-defined priorities can be styled with the setPriorityStyle() method.

<?php
$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.

<?php
$writer
->setDefaultPriorityStyle('TRACE');

The supported styles are as follows:

Table 114. 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


Preparing data for Logging

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.

Exception Logging

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.

<?php
$exception 
= new Zend_Exception('Test exception');
$logger->err($exception);

Table Logging

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.

<?php
$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 Framework