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

Skip to content

Commit 28a2fd0

Browse files
authored
gh-115032: Deprecate support for custom logging handlers with 'strm' argument. (GH-115314)
1 parent 355ee1a commit 28a2fd0

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Doc/whatsnew/3.14.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1580,6 +1580,11 @@ Deprecated
15801580
or *sequence* as keyword arguments is now deprecated.
15811581
(Contributed by Kirill Podoprigora in :gh:`121676`.)
15821582

1583+
* :mod:`logging`:
1584+
Support for custom logging handlers with the *strm* argument is deprecated
1585+
and scheduled for removal in Python 3.16. Define handlers with the *stream*
1586+
argument instead. (Contributed by Mariusz Felisiak in :gh:`115032`.)
1587+
15831588
* :mod:`!nturl2path`: This module is now deprecated. Call
15841589
:func:`urllib.request.url2pathname` and :func:`~urllib.request.pathname2url`
15851590
instead.

Lib/logging/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,8 @@ def configure_handler(self, config):
865865
else:
866866
factory = klass
867867
kwargs = {k: config[k] for k in config if (k != '.' and valid_ident(k))}
868+
# When deprecation ends for using the 'strm' parameter, remove the
869+
# "except TypeError ..."
868870
try:
869871
result = factory(**kwargs)
870872
except TypeError as te:
@@ -876,6 +878,15 @@ def configure_handler(self, config):
876878
#(e.g. by Django)
877879
kwargs['strm'] = kwargs.pop('stream')
878880
result = factory(**kwargs)
881+
882+
import warnings
883+
warnings.warn(
884+
"Support for custom logging handlers with the 'strm' argument "
885+
"is deprecated and scheduled for removal in Python 3.16. "
886+
"Define handlers with the 'stream' argument instead.",
887+
DeprecationWarning,
888+
stacklevel=2,
889+
)
879890
if formatter:
880891
result.setFormatter(formatter)
881892
if level is not None:

Lib/test/test_logging.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3281,6 +3281,37 @@ def format(self, record):
32813281
}
32823282
}
32833283

3284+
# Remove when deprecation ends.
3285+
class DeprecatedStrmHandler(logging.StreamHandler):
3286+
def __init__(self, strm=None):
3287+
super().__init__(stream=strm)
3288+
3289+
config_custom_handler_with_deprecated_strm_arg = {
3290+
"version": 1,
3291+
"formatters": {
3292+
"form1": {
3293+
"format": "%(levelname)s ++ %(message)s",
3294+
},
3295+
},
3296+
"handlers": {
3297+
"hand1": {
3298+
"class": DeprecatedStrmHandler,
3299+
"formatter": "form1",
3300+
"level": "NOTSET",
3301+
"stream": "ext://sys.stdout",
3302+
},
3303+
},
3304+
"loggers": {
3305+
"compiler.parser": {
3306+
"level": "DEBUG",
3307+
"handlers": ["hand1"],
3308+
},
3309+
},
3310+
"root": {
3311+
"level": "WARNING",
3312+
},
3313+
}
3314+
32843315
def apply_config(self, conf):
32853316
logging.config.dictConfig(conf)
32863317

@@ -3370,6 +3401,15 @@ def test_config5_ok(self):
33703401
self.test_config1_ok(config=self.config5)
33713402
self.check_handler('hand1', CustomHandler)
33723403

3404+
def test_deprecation_warning_custom_handler_with_strm_arg(self):
3405+
msg = (
3406+
"Support for custom logging handlers with the 'strm' argument "
3407+
"is deprecated and scheduled for removal in Python 3.16. "
3408+
"Define handlers with the 'stream' argument instead."
3409+
)
3410+
with self.assertWarnsRegex(DeprecationWarning, msg):
3411+
self.test_config1_ok(config=self.config_custom_handler_with_deprecated_strm_arg)
3412+
33733413
def test_config6_failure(self):
33743414
self.assertRaises(Exception, self.apply_config, self.config6)
33753415

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Support for custom logging handlers with the *strm* argument is deprecated
2+
and scheduled for removal in Python 3.16. Define handlers with the *stream*
3+
argument instead. Patch by Mariusz Felisiak.

0 commit comments

Comments
 (0)