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');
blog comments powered by Disqus