Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-115032: Deprecate support for custom logging handlers with 'strm' …
…argument.
  • Loading branch information
felixxm committed Apr 26, 2025
commit 9f1a63eb85f70fd711f6fce566661daebee24705
11 changes: 11 additions & 0 deletions Lib/logging/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,8 @@ def configure_handler(self, config):
else:
factory = klass
kwargs = {k: config[k] for k in config if (k != '.' and valid_ident(k))}
# When deprecation ends for using the 'strm' parameter, remove the
# "except TypeError ..."
try:
result = factory(**kwargs)
except TypeError as te:
Expand All @@ -876,6 +878,15 @@ def configure_handler(self, config):
#(e.g. by Django)
kwargs['strm'] = kwargs.pop('stream')
result = factory(**kwargs)

import warnings
warnings.warn(
"Support for custom logging handlers with the 'strm' argument "
"is deprecated and scheduled for removal in Python 3.15. "
"Define handlers with the 'stream' argument instead.",
DeprecationWarning,
stacklevel=2,
)
if formatter:
result.setFormatter(formatter)
if level is not None:
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3281,6 +3281,37 @@ def format(self, record):
}
}

# Remove when deprecation ends.
class DeprecatedStrmHandler(logging.StreamHandler):
def __init__(self, strm=None):
super().__init__(stream=strm)

config_custom_handler_with_deprecated_strm_arg = {
"version": 1,
"formatters": {
"form1": {
"format": "%(levelname)s ++ %(message)s",
},
},
"handlers": {
"hand1": {
"class": DeprecatedStrmHandler,
"formatter": "form1",
"level": "NOTSET",
"stream": "ext://sys.stdout",
},
},
"loggers": {
"compiler.parser": {
"level": "DEBUG",
"handlers": ["hand1"],
},
},
"root": {
"level": "WARNING",
},
}

def apply_config(self, conf):
logging.config.dictConfig(conf)

Expand Down Expand Up @@ -3370,6 +3401,15 @@ def test_config5_ok(self):
self.test_config1_ok(config=self.config5)
self.check_handler('hand1', CustomHandler)

def test_deprecation_warning_custom_handler_with_strm_arg(self):
msg = (
"Support for custom logging handlers with the 'strm' argument "
"is deprecated and scheduled for removal in Python 3.15. "
"Define handlers with the 'stream' argument instead."
)
with self.assertWarnsRegex(DeprecationWarning, msg):
self.test_config1_ok(config=self.config_custom_handler_with_deprecated_strm_arg)

def test_config6_failure(self):
self.assertRaises(Exception, self.apply_config, self.config6)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Support for custom logging handlers with the *strm* argument is deprecated
and scheduled for removal in Python 3.15. Define handlers with the *stream*
argument instead. Patch by Mariusz Felisiak.