Skip to content

Commit fee2731

Browse files
committed
pythongh-115032: Deprecate support for custom logging handlers with 'strm' argument.
1 parent 2939ad0 commit fee2731

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Doc/whatsnew/3.13.rst

+5
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,11 @@ Deprecated
629629
security and functionality bugs. This includes removal of the ``--cgi``
630630
flag to the ``python -m http.server`` command line in 3.15.
631631

632+
* :mod:`logging`: Support for custom logging handlers with the *strm* argument
633+
is deprecated and scheduled for removal in Python 3.15. Define handlers with
634+
the *stream* argument instead.
635+
(Contributed by Mariusz Felisiak in :gh:`115032`.)
636+
632637
* :mod:`pathlib`:
633638
:meth:`pathlib.PurePath.is_reserved` is deprecated and scheduled for
634639
removal in Python 3.15. Use :func:`os.path.isreserved` to detect reserved

Lib/logging/config.py

+11
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,8 @@ def configure_handler(self, config):
838838
else:
839839
factory = klass
840840
kwargs = {k: config[k] for k in config if (k != '.' and valid_ident(k))}
841+
# When deprecation ends for using the 'strm' parameter, remove the
842+
# "except TypeError ..."
841843
try:
842844
result = factory(**kwargs)
843845
except TypeError as te:
@@ -849,6 +851,15 @@ def configure_handler(self, config):
849851
#(e.g. by Django)
850852
kwargs['strm'] = kwargs.pop('stream')
851853
result = factory(**kwargs)
854+
855+
import warnings
856+
warnings.warn(
857+
"Support for custom logging handlers with the 'strm' argument "
858+
"is deprecated and scheduled for removal in Python 3.15. "
859+
"Define handlers with the 'stream' argument instead.",
860+
DeprecationWarning,
861+
stacklevel=2,
862+
)
852863
if formatter:
853864
result.setFormatter(formatter)
854865
if level is not None:

Lib/test/test_logging.py

+40
Original file line numberDiff line numberDiff line change
@@ -3214,6 +3214,37 @@ def format(self, record):
32143214
}
32153215
}
32163216

3217+
# Remove when deprecation ends.
3218+
class DeprecatedStrmHandler(logging.StreamHandler):
3219+
def __init__(self, strm=None):
3220+
super().__init__(stream=strm)
3221+
3222+
config_custom_handler_with_deprecated_strm_arg = {
3223+
'version': 1,
3224+
'formatters': {
3225+
'form1' : {
3226+
'format' : '%(levelname)s ++ %(message)s',
3227+
},
3228+
},
3229+
'handlers' : {
3230+
'hand1' : {
3231+
'class' : DeprecatedStrmHandler,
3232+
'formatter' : 'form1',
3233+
'level' : 'NOTSET',
3234+
'stream' : 'ext://sys.stdout',
3235+
},
3236+
},
3237+
'loggers' : {
3238+
'compiler.parser' : {
3239+
'level' : 'DEBUG',
3240+
'handlers' : ['hand1'],
3241+
},
3242+
},
3243+
'root' : {
3244+
'level' : 'WARNING',
3245+
},
3246+
}
3247+
32173248
def apply_config(self, conf):
32183249
logging.config.dictConfig(conf)
32193250

@@ -3303,6 +3334,15 @@ def test_config5_ok(self):
33033334
self.test_config1_ok(config=self.config5)
33043335
self.check_handler('hand1', CustomHandler)
33053336

3337+
def test_deprecation_warning_custom_handler_with_strm_arg(self):
3338+
msg = (
3339+
"Support for custom logging handlers with the 'strm' argument "
3340+
"is deprecated and scheduled for removal in Python 3.15. "
3341+
"Define handlers with the 'stream' argument instead."
3342+
)
3343+
with self.assertWarnsRegex(DeprecationWarning, msg):
3344+
self.test_config1_ok(config=self.config_custom_handler_with_deprecated_strm_arg)
3345+
33063346
def test_config6_failure(self):
33073347
self.assertRaises(Exception, self.apply_config, self.config6)
33083348

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.15. Define handlers with the *stream*
3+
argument instead. Patch by Mariusz Felisiak.

0 commit comments

Comments
 (0)