New submission from raybb <rayz3...@gmail.com>:

According to the docs here (https://docs.python.org/3/library/logging.html):

"You don’t need to create specialized Filter classes, or use other classes with 
a filter method: you can use a function (or other callable) as a filter. The 
filtering logic will check to see if the filter object has a filter attribute: 
if it does, it’s assumed to be a Filter and its filter() method is called. 
Otherwise, it’s assumed to be a callable and called with the record as the 
single parameter."


If I use this code:

def noErrorLogs(param):
    return 1 if param.levelno < 40 else 0

logconfig_dict = {
    'filters': {
        'myfilter': {
            '()': noErrorLogs,
        }
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "stream": "ext://sys.stdout",
            "filters": ["myfilter"]
        }
    },
    "root": {"level": "DEBUG", "handlers": ["console"]},
    "version": 1,
}
dictConfig(logconfig_dict)

I get the error "Unable to configure filter 'myfilter'" because "noErrorLogs() 
missing 1 required positional argument: 'param'"


However, If I use this code:


def noErrorLogs(param):
    return 1 if param.levelno < 40 else 0

logconfig_dict = {
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "level": "DEBUG",
            "stream": "ext://sys.stdout",
        }
    },
    "root": {"level": "DEBUG", "handlers": ["console"]},
    "version": 1,
}

logging.basicConfig()
dictConfig(logconfig_dict)
l = logging.getLogger()
l.handlers[0].addFilter(noErrorLogs)

Then the filter works correctly.




The bug I am reporting is that when using logging.config.dictConfig you cannot 
pass in a callable that acts a filter. You can only pass in a class with a 
filter method or a function that returns a callable filter.

If this is the expected behavior perhaps the docs should make it clear that 
callables cannot be used with dictConfig.

----------
messages: 377791
nosy: raybb
priority: normal
severity: normal
status: open
title: logging.config.dictConfig does not work with callable filters
type: behavior

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue41906>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to