python logging filters

Vinay Sajip vinay_sajip at yahoo.co.uk
Sat Nov 28 04:26:28 EST 2009


On Nov 27, 1:11 pm, Grimsqueaker <grimsqueake... at gmail.com> wrote:
> When I add a Filter to a Handler, everything works as expected (ie.
> all messages sent from Loggers below the Filter's level are allowed
> through), but when I add the Filter directly on to the Logger, only
> that Logger is blocked, regardless of the contents of the Filter.

The key thing to remember is that when a logger processes an event,
handlers attached to it *and all its parents* are offered the event
for handling. In the case where you have just one handler and it has
the filter attached, filtering works as you expected. By attaching the
filter to the root logger, you are not filtering its handler; this
handler is invoked for events logged to all the other loggers, and so
(apart from the event at the root logger) those events are not
filtered.

For some examples of filter usage, see this post:

http://groups.google.com/group/comp.lang.python/msg/2eb4cf8f879c6451

Regards,

Vinay Sajip



More information about the Python-list mailing list