This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: logging filter is not applied to messages from descendant loggers
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.6
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: nikratio, vinay.sajip
Priority: normal Keywords:

Created on 2010-08-14 21:38 by nikratio, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (2)
msg113932 - (view) Author: Nikolaus Rath (nikratio) * Date: 2010-08-14 21:38
I believe the following should print only one log message, not two:

import logging
class Filter(object):
    def filter(self, record):
        return record.name == 'foo'  
logger = logging.getLogger()
formatter = logging.Formatter('%(message)s') 
handler = logging.StreamHandler()
handler.setFormatter(formatter)
logger.addFilter(Filter())
logger.addHandler(handler)
logging.getLogger('foo').warn('foo!')
logging.getLogger('bar').warn('bar!')

If the filter is added to the handler instead of the logger, everything works as expected.

If this is desired behaviour, please consider this a bug against the documentation. In that case I propose to extend the documentation of Logger.addFilter() as follows: "Note that the filter will act only on log messages that are generated by this logger, and not on messages that have been generated by descendant loggers."
msg114407 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2010-08-19 19:18
The following paragraph has been added to the documentation for Filter objects in py3k and release27-maint branches (r84212):

"Note that filters attached to handlers are consulted whenever an event is emitted by the handler, whereas filters attached to loggers are consulted whenever an event is logged to the handler (using debug(), info(), etc.) This means that events which have been generated by descendant loggers will not be filtered by a logger's filter setting, unless the filter has also been applied to those descendant loggers."
History
Date User Action Args
2022-04-11 14:57:05adminsetgithub: 53815
2010-08-19 19:18:41vinay.sajipsetstatus: open -> closed

nosy: + vinay.sajip
messages: + msg114407

resolution: fixed
2010-08-14 21:39:31nikratiosettitle: logging filter is ignored when added to root handler -> logging filter is not applied to messages from descendant loggers
2010-08-14 21:38:22nikratiocreate