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

Zend Framework 101: Zend_Log

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.

Listing 3 Filtering messages by priority level (listing-3.php)
    // 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.

Listing 4 Sample log output when filtering by priority (listing-4.txt)
$ 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.

In This Article