[New-bugs-announce] [issue7535] logging: Filters on Loggers can't actually filter messages on lower levels of the logging hierarchy
Leonardo Rochael Almeida
report at bugs.python.org
Thu Dec 17 19:52:15 CET 2009
New submission from Leonardo Rochael Almeida <leorochael at gmail.com>:
A filter added to, say, logging.getLogger('a').addFilter() never gets
the chance to filter messages logged into logging.getLogger('a.b').
On logging.Logger.handle() the current logger filters are called to
check if the message should be processed or not, but the logger parent
filters are not consulted (and this is ok, IMHO, more generic filters
shouldn't clobber messages of more specific loggers).
However, if none of the current filters block the message,
Logger.handle() then calls Logger.callHandlers(), which calls the
handlers of the current logger and of all the parent loggers as long as
each logger in the hierarchy allows the message to propagate. This is
done without consulting the parent loggers filters.
This means parent handlers get called to process log records even if the
parent logger filters would have otherwise blocked them.
Attached is a very crude patch that allows the parent loggers to filter
events before handling them. I'd prefer to simply call the "handle()"
or "callHandler()" method of the parent loggers recursively, but
couldn't find a way to do this while still preserving the method
signatures AND properly reporting when no handler could be found.
My preference would be to have callHandlers() do the filtering AND
return a number of handlers invoked, including the parent handlers, so
that handle() could report if no handlers could be found.
components: Library (Lib)
title: logging: Filters on Loggers can't actually filter messages on lower levels of the logging hierarchy
versions: Python 2.4, Python 2.5, Python 2.6, Python 2.7
Added file: http://bugs.python.org/file15583/logging-allow-parent-filtering.diff
Python tracker <report at bugs.python.org>
More information about the New-bugs-announce