Zend Framework 101: Zend_Log


This article shows you how to use Zend_Log, the logging component of the Zend Framework. It allows you to record messages from your application however you please. In this article I will show you how to record messages to a log file.

Additionally, if you use the Firebug and FirePHP plug-ins for Firefox, Zend_Log can be extremely useful for application development and debugging. I will also show you how to achieve this.

This article requires Zend Framework, downloadable from http://framework.zend.com. At time of writing, the current version of Zend Framework is 1.7.8. Additionally, later in the article you will need to be using Firefox with the Firebug and FirePHP plug-ins.

Creating Your First Logger

To begin, I’ll show you how to create a basic file-system logger. To achieve this we must instantiate the Zend_Log class, then add a writer to this object. We will use a writer that writes log messages to the file system.

The class we use to write log messages to the file system is Zend_Log_Writer_Stream. This class accepts the path where the log messages should be written to as the first argument to the constructor.

Note: The path you use must be writable by the web server.

Listing 1 shows an example of creating the logger and adding the writer to it. You can either add the writer when instantiating Zend_Log (such as new Zend_Log(new Zend_Log_Writer_Stream(…))), or as shown in the following listing.




    $logger = new Zend_Log();

    $writer = new Zend_Log_Writer_Stream(‘sample.log’);


    $logger->log(‘sample message!’, Zend_Log::INFO);


The above listing concludes by calling the log() method on the Zend_Log object. This records a message to the log. Listing 2 shows an example of how the log file appears after this code is run.

$ cat sample.log

2009-04-11T14:47:20+09:00 INFO (6): sample message!

In the next section I’ll cover the recording of events in greater detail.

Recording Events

As we briefly saw in the previous section, you can record a message to the log using the log() method. The first argument is the message you want to log while the second argument is the priority level of the message.

By default, the priorities are as follows:

  • Zend_Log::EMERG: Emergency: system is unusable
  • Zend_Log::ALERT: Alert: action must be taken immediately
  • Zend_Log::CRIT: Critical: critical conditions
  • Zend_Log::ERR: Error: error conditions
  • Zend_Log::WARN: Warning: warning conditions
  • Zend_Log::NOTICE: Notice: normal but significant condition
  • Zend_Log::INFO: Informational: informational messages
  • Zend_Log::DEBUG: Debug: debug messages

You can pass any of these constants as the second argument to log(), or you can use one of the helper methods provided by Zend_Log. Previously we used $logger->log(‘message’, Zend_Logger::INFO), but Zend_Log also allows us to use the shorthand method of $logger->info(‘message’);

Messages for other priorities can also be recorded in this manner. For instance, $logger->warn(‘Some Warning’) instead of $logger->log(‘Some Warning’, Zend_Log::WARN).

Tip: You can also add your own priorities using the addPriority() method.

Filtering Events

If you want your log writer to display only messages of a certain type, you can filter by priority. This is useful in the case where you might recorded informational messages and error messages. Typically you will only want to know about the error messages, but you may want to enable all informational messages while trying to debug error messages.

Events are filtered using the Zend_Log_Filter_Priority class. You can add an instance of this class either to a writer (to filter only that writer) or add it to the logger to filter all writers.

Once the priority filter has been created you can add it to the writer using the addFilter() method.

Listing 3 shows an example of filtering so only critical messages are recorded. In the example we record several messages.




    // create the logger

    $logger = new Zend_Log();

    // create the writer

    $writer = new Zend_Log_Writer_Stream(‘filters.log’);

    // create the priority filter

    $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);

    // add the filter to the writer


    // add the writer to the logger


    // record messages

    $logger->info(‘info message’);

    $logger->crit(‘critical message’);


If you look at the log as shown in Listing 4, only the critical message was record.

$ cat filters.log

2009-04-11T15:01:52+09:00 CRIT (2): critical message

In addition to the priority filter there are other filters available also, such as the Zend_Log_Filter_Message filter which allows you to filter messages by regular expression.

Debugging With Firebug and FirePHP

Zend_Log contains a log writer that will write messages to the HTTP headers of your web page response, which allows the Firebug plug-in for Firefox to show your log messages. This is an extremely powerful development tool, both for debugging and profiling your applications.

Note: This functionality can also be used easily with Zend_Db when profiling SQL queries, however, this is not covered in this article.

In order for this functionality to work you must be using Firefox with the following plug-ins installed:

You must then use the bundled Zend_Log_Writer_Firebug writer. Listing 5 shows how this is achieved.




    $writer = new Zend_Log_Writer_Firebug();

    $logger = new Zend_Log($writer);

    $logger->info(‘info message’);

    $logger->warn(‘warning message’);

    $logger->err(‘error message’);


Note however that unless you’re using Zend_Controller_Front with your site then nothing will happen when you use this code, as described in the next section.

Sending Log Messages

If you’re using Zend_Controller_Front to manage requests to your web site then any log messages will automatically be sent to your browser in the HTTP headers.

If not, you must manually get the messages to be sent to your browser. The Zend_Log_Writer_Firebug class makes use of the Zend_Wildfire component, so you must tell this class about your HTTP response so headers can be inserted.

We must use the Zend_Controller_Request_Http and Zend_Controller_Response_Http classes and tell Zend_Wildfire about the request and response. This is so once the request has finished Zend_Wildfire will write the HTTP headers containing the logging information.

Listing 6 shows how we achieve this. The $channel->flush() call near the results in the headers being inserted into the response. We then output the headers using sendHeaders().






    // create the logger and log writer

    $writer = new Zend_Log_Writer_Firebug();

    $logger = new Zend_Log($writer);

    // get the wildfire channel

    $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();

    // create and set the HTTP response

    $response = new Zend_Controller_Response_Http();



    // create and set the HTTP request

    $channel->setRequest(new Zend_Controller_Request_Http());

    // record log messages

    $logger->info(‘info message’);

    $logger->warn(‘warning message’);

    $logger->err(‘error message’);

    // insert the wildfire headers into the HTTP response



    // send the HTTP response headers



If you then view this script in your browser (with Firebug and FirePHP enabled) you see the following.

Notice that the native Firebug message type indicators are used, and that the error also resulted in “1 error” being displayed at the bottom-right.


In this article I introduced you to the Zend_Log component of the Zend Framework and how to use it. Additionally I showed you how you can use Zend_Log to interact directly with your browser.

Further reading:

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *