Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d7c6863

Browse files
authored
bpo-41906: Accept built filters in dictConfig (GH-30756)
When configuring the logging stack, accept already built filters (or just callables) in the filters array of loggers and handlers. This facilitates passing quick callables as filters. Automerge-Triggered-By: GH:vsajip
1 parent 58f3d98 commit d7c6863

4 files changed

Lines changed: 55 additions & 1 deletion

File tree

Doc/library/logging.config.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ otherwise, the context is used to determine what to instantiate.
288288
* ``filters`` (optional). A list of ids of the filters for this
289289
handler.
290290

291+
.. versionchanged:: 3.11
292+
``filters`` can take filter instances in addition to ids.
293+
291294
All *other* keys are passed through as keyword arguments to the
292295
handler's constructor. For example, given the snippet:
293296

@@ -326,6 +329,9 @@ otherwise, the context is used to determine what to instantiate.
326329
* ``filters`` (optional). A list of ids of the filters for this
327330
logger.
328331

332+
.. versionchanged:: 3.11
333+
``filters`` can take filter instances in addition to ids.
334+
329335
* ``handlers`` (optional). A list of ids of the handlers for this
330336
logger.
331337

@@ -524,6 +530,10 @@ valid keyword parameter name, and so will not clash with the names of
524530
the keyword arguments used in the call. The ``'()'`` also serves as a
525531
mnemonic that the corresponding value is a callable.
526532

533+
.. versionchanged:: 3.11
534+
The ``filters`` member of ``handlers`` and ``loggers`` can take
535+
filter instances in addition to ids.
536+
527537

528538
.. _logging-config-dict-externalobj:
529539

Lib/logging/config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,11 @@ def add_filters(self, filterer, filters):
694694
"""Add filters to a filterer from a list of names."""
695695
for f in filters:
696696
try:
697-
filterer.addFilter(self.config['filters'][f])
697+
if callable(f) or callable(getattr(f, 'filter', None)):
698+
filter_ = f
699+
else:
700+
filter_ = self.config['filters'][f]
701+
filterer.addFilter(filter_)
698702
except Exception as e:
699703
raise ValueError('Unable to add filter %r' % f) from e
700704

Lib/test/test_logging.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3447,6 +3447,44 @@ def emit(self, record):
34473447
logging.info('some log')
34483448
self.assertEqual(stderr.getvalue(), 'some log my_type\n')
34493449

3450+
def test_config_callable_filter_works(self):
3451+
def filter_(_):
3452+
return 1
3453+
self.apply_config({
3454+
"version": 1, "root": {"level": "DEBUG", "filters": [filter_]}
3455+
})
3456+
assert logging.getLogger().filters[0] is filter_
3457+
logging.getLogger().filters = []
3458+
3459+
def test_config_filter_works(self):
3460+
filter_ = logging.Filter("spam.eggs")
3461+
self.apply_config({
3462+
"version": 1, "root": {"level": "DEBUG", "filters": [filter_]}
3463+
})
3464+
assert logging.getLogger().filters[0] is filter_
3465+
logging.getLogger().filters = []
3466+
3467+
def test_config_filter_method_works(self):
3468+
class FakeFilter:
3469+
def filter(self, _):
3470+
return 1
3471+
filter_ = FakeFilter()
3472+
self.apply_config({
3473+
"version": 1, "root": {"level": "DEBUG", "filters": [filter_]}
3474+
})
3475+
assert logging.getLogger().filters[0] is filter_
3476+
logging.getLogger().filters = []
3477+
3478+
def test_invalid_type_raises(self):
3479+
class NotAFilter: pass
3480+
for filter_ in [None, 1, NotAFilter()]:
3481+
self.assertRaises(
3482+
ValueError,
3483+
self.apply_config,
3484+
{"version": 1, "root": {"level": "DEBUG", "filters": [filter_]}}
3485+
)
3486+
3487+
34503488
class ManagerTest(BaseTest):
34513489
def test_manager_loggerclass(self):
34523490
logged = []
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Support passing filter instances in the ``filters`` values of ``handlers`` and
2+
``loggers`` in the dictionary passed to :func:`logging.config.dictConfig`.

0 commit comments

Comments
 (0)