Zend Framework 101: Zend_Log
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
Listing 3 shows an example of filtering so only critical messages are recorded. In the example we record several messages.
require_once('Zend/Log.php'); require_once('Zend/Log/Writer/Stream.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 $writer->addFilter($filter); // add the writer to the logger $logger->addWriter($writer); // 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.