python logging filters

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Nov 30 09:10:41 EST 2009


On Nov 30, 6:52 am, Grimsqueaker <grimsqueake... at gmail.com> wrote:
>
> So would I be correct in saying that Filters apply only the the object
> they are attached to and have no effect on how messages are propagated
> through thelogginghierarchy? If this is the case my next question
> would be: How can I affect whether or not a message is propagated
> further up the tree?
>

You are correct about how Filters work. To prevent propagation, use
the propagate attribute, documented here:

http://docs.python.org/library/logging.html#logger-objects

Most times, you don't need to use this. For example, if you want
events in the A hierarchy to go to one place and events in the B
hierarchy to go to another place (e.g. two different log files), you
just attach different handlers (e.g. two different FileHandlers) to
loggers A and B. If you want a combined log as well, add an
appropriate handler to the root logger.

I find that things usually work OK if I just determine what handlers I
need and attach them to the appropriate loggers, setting the levels on
handlers and loggers appropriately. If I need filtering logic for a
logger or handler which is more involved than just an integer
threshold (which happens less often), I use Filters. Even less often,
if I need to block events from a part of the logger hierarchy from
bubbling upwards, then I use the propagate attribute.

Regards,

Vinay Sajip



More information about the Python-list mailing list