selective logger disable/enable

Vinay Sajip vinay_sajip at yahoo.co.uk
Mon Jan 22 00:09:56 EST 2007


Gary Jefferson wrote:

> I am still a bit confused about Filters, though.  It seems they are a
> bit of an anomoly in the hierarchical view of loggers that the API
> supports elsewhere, i.e., filters don't seem to inherit...  Or am I
> missing something again?  Here's a quick example:
>
> import logging
>
> log1 = logging.getLogger("top")
> log2 = logging.getLogger("top.network")
> log3 = logging.getLogger("top.network.tcp")
> log4 = logging.getLogger("top.network.http")
> log5 = logging.getLogger("top.config")
> log6 = logging.getLogger("top.config.file")
>
> logging.basicConfig(level=logging.DEBUG,
>         format='%(asctime)s %(levelname)s %(message)s')
>
> filter = logging.Filter("top.network")
> log1.addFilter(filter)  # only affects log1, do this for each of log2-7
> too?
>
> log1.debug("I'm top")
> log2.debug("I'm top.network")
> log3.debug("I'm top.network.tcp")
> log4.debug("I'm top.network.http")
> log5.debug("I'm top.config")
> log6.debug("I'm top.config.file")
>
>
> This is only for the binary case (and I think if I ignore the binary
> case and filters altogether as you suggested), but it really would be
> nice to be able to squelch /all/ output from loggers that belong to
> certain parts of the namespace by using a filter as above (which I
> can't get to work).
>
> Perhaps if I set up a basicConfig with a loglevel of nothing, I can get
> this to approximate squelching of everything but that which I
> explicitly setLevel (which does inherit properly).
>
> In other words, the addFilter/removeFilter part of the API seems rather
> impotent if it can't be inherited in the logging namespaces.  In fact,
> I can't really figure out a use case where I could possibly want to use
> it without it inheriting.  Obviously I'm missing something.  I'm sure
> I've consumed more attention that I deserve already in this thread,
> but, do you have any pointers which can enlighten me as to how to
> effectively use addFilter/removeFilter?

I don't really think there's a problem with the logging API - it went
through a fair amount of peer review on python-dev before making it
into the Python distribution. You need to use what's there rather than
shoehorn it into how you think it ought to be. Filters are for more
esoteric requirements - you can see some examples of filters in the old
(out of date) standalone distribution at
http://www.red-dove.com/python_logging.html (download and examine some
of the test scripts). Loggers aren't binary - levels are there to be
used. Most people get by with judicious use of levels, using Filters on
loggers only for unusual cases. Since Filters are only meant to be used
in unusual situations, there is no need to think about inheriting them.

Filters can be set on Handlers so that even if Loggers log the events,
they don't go to any output if filtered out at the handlers for that
output.

BTW I would also advise reading PEP-282 to understand more about the
logging approach.

Best regards,

Vinay Sajip




More information about the Python-list mailing list