Programmer's Reference Guide

Formatters

Filters

A Filter object blocks a message from being written to the log.

Filtering for All Writers

To filter before all writers, you can add any number of Filters to a Log object using the addFilter() method:

  1. $logger = new Zend_Log();
  2.  
  3. $writer = new Zend_Log_Writer_Stream('php://output');
  4. $logger->addWriter($writer);
  5.  
  6. $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
  7. $logger->addFilter($filter);
  8.  
  9. // blocked
  10. $logger->info('Informational message');
  11.  
  12. // logged
  13. $logger->emerg('Emergency message');

When you add one or more Filters to the Log object, the message must pass through all of the Filters before any Writers receives it.

Filtering for a Writer Instance

To filter only on a specific Writer instance, use the addFilter() method of that Writer:

  1. $logger = new Zend_Log();
  2.  
  3. $writer1 = new Zend_Log_Writer_Stream('/path/to/first/logfile');
  4. $logger->addWriter($writer1);
  5.  
  6. $writer2 = new Zend_Log_Writer_Stream('/path/to/second/logfile');
  7. $logger->addWriter($writer2);
  8.  
  9. // add a filter only to writer2
  10. $filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
  11. $writer2->addFilter($filter);
  12.  
  13. // logged to writer1, blocked from writer2
  14. $logger->info('Informational message');
  15.  
  16. // logged by both writers
  17. $logger->emerg('Emergency message');

Formatters

Comments

Correct me if I am wrong but in the above examples of adding filters, the code:

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

should be

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

when used with

$logger->emerg('Emergency Message');

------ also -------

"A Filter object blocks a message from being written to the log."

This was missleading for me as I found it to do the opposite of this.

Would it make more sense if it read:

"When filters are used only filtered priorities will be written to the log."



* @param string $operator Comparison operator
public function __construct($priority, $operator = NULL)

You can set your own comparison operator, by default its <=
version_compare php function is used to check if log entry passes the comparison and goes to certain writer/s

+ Add A Comment

Please do not report issues via comments; use the ZF Issue Tracker.

If you have a JIRA/Crowd account, we suggest you login first before commenting.

  • BBCode is allowed in the comment markup

  • Select a Version

    Languages Available

    Components

    Search the Manual