Zend_Log timestamp filter

      Comments Off on Zend_Log timestamp filter

For one of my recent project, which is using Zend_Log component of the Zend Framework, I had a demand in which I needed to be able to filter log events based on the time they occurred. As out of the box, Zend_Log component does not have such filter, I decided to create one that will fulfill my demands.

NP_Log_Filter_Timestamp is a custom Zend_Log filter, which provides ability for filtering log events based on their timestamp property, or in other words, time when they occurred. Rules for filtering can be set for various date/time parts, for example: day, month, hour, minute, day of the week, etc., due to fact that my filter utilize PHP idate() function. Here is a direct link for downloading NP_Log_Filter_Timestamp package.

Each NP_Log_Filter_Timestamp instance represents one rule for filtering log events based on the date/time they were triggered. Developer can add any number of filters to a Log object, as described here.

Filtering rule is defined using three parameters:

  • format – represents format character which is supported by PHP idate() function,
  • value – desired value of some date/time part, which will be used for comparison,
  • operator – comparison operator (for example: ==, <, >=, etc.) that defines how some part of the log event’s timestamp (calculated using “format”), should be compared against a desired “value”

Here’s an example of how instance of this filter can be created:

This filter will accept log entry events which occur on Monday (w EQ 1). EQ is of course a constant, which represents comparison operator, in this case “==”.

It is also possible to combine multiple instances of NP_Log_Filter_Timestamp filter,and add them to some Writer, in order to achieve more complex filtering rule:

This writer will only log events which occur on Monday (first filter – w EQ 1)
and on every month except March (second filter – m NEQ 3).

Download NP_Log_Filter_Timestamp