python loggingL filter limitation, looks intentional?

Vinay Sajip vinay_sajip at
Mon Jan 16 18:21:55 EST 2012

> Why is this? There must be some rationale for this rather than what, for me and 

> others I've talked to, would seem more natural, ie: a filter on the root 
> logger would get messages both logged to it and any messages propagated to it 
> from child loggers to process.

Perhaps you're right, but this can't be changed without breaking existing code, AFAICT. This hasn't come up before, so I'm not sure to what extent this is just a matter of taste.

>>  However, if you find your usage pattern occurring very commonly in your 
> applications, it's easy enough to create a DelegatingHandler handler class 
> which passes its events on to any number of handlers you like.
> But this is just extra work when the normal built-in 
> delegation-upwards-ending-in-root works exactly as I want.

It's not in general useful to filter on message content, when one factors in use of third-party libraries. Of course, if you are in control in the whole software stack you're using, or in certain very specific scenarios, it may be more useful - but in general it's most useful to filter on level and using logger names. The __name__ for loggers is useful to locate the source of events to a module, but you can have child loggers under that to log specific types of events in that module, and use the verbosity of those sub-module loggers to control output.

Of course I don't know the precise reason you need things to work this way, so my suggestions and comments might appear somewhat general.

> It's also the opposite of what I'd need, I'd need to plug one 
> handler into all the specific loggers that get used (and bear in mind that many 
> libraries log to __name__ from modules and sub-modules) this is a pretty onerous 
> task :-(

I thought from an earlier comment - "rather than just the root logger where my handlers live" - that you had your handlers attached to the root logger, in which case it wouldn't be onerous at all. In place of those individual handlers attached to the root, you simply attach your DelegatingHandler to the root logger, and attach the filter and the other handlers to that DelegatingHandler instance.


Vinay Sajip

More information about the Python-list mailing list