Skip to content

Commit 6d0bb43

Browse files
authored
gh-117975: Ensure flush level is checked when configuring a logging MemoryHandler. (GH-117976)
1 parent b9b3c45 commit 6d0bb43

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

Lib/logging/config.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,20 @@ def configure_handler(self, config):
761761
klass = cname
762762
else:
763763
klass = self.resolve(cname)
764-
if issubclass(klass, logging.handlers.MemoryHandler) and\
765-
'target' in config:
766-
# Special case for handler which refers to another handler
767-
try:
768-
tn = config['target']
769-
th = self.config['handlers'][tn]
770-
if not isinstance(th, logging.Handler):
771-
config.update(config_copy) # restore for deferred cfg
772-
raise TypeError('target not configured yet')
773-
config['target'] = th
774-
except Exception as e:
775-
raise ValueError('Unable to set target handler %r' % tn) from e
764+
if issubclass(klass, logging.handlers.MemoryHandler):
765+
if 'flushLevel' in config:
766+
config['flushLevel'] = logging._checkLevel(config['flushLevel'])
767+
if 'target' in config:
768+
# Special case for handler which refers to another handler
769+
try:
770+
tn = config['target']
771+
th = self.config['handlers'][tn]
772+
if not isinstance(th, logging.Handler):
773+
config.update(config_copy) # restore for deferred cfg
774+
raise TypeError('target not configured yet')
775+
config['target'] = th
776+
except Exception as e:
777+
raise ValueError('Unable to set target handler %r' % tn) from e
776778
elif issubclass(klass, logging.handlers.QueueHandler):
777779
# Another special case for handler which refers to other handlers
778780
# if 'handlers' not in config:

Lib/test/test_logging.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3036,6 +3036,30 @@ def format(self, record):
30363036
},
30373037
}
30383038

3039+
config18 = {
3040+
"version": 1,
3041+
"handlers": {
3042+
"console": {
3043+
"class": "logging.StreamHandler",
3044+
"level": "DEBUG",
3045+
},
3046+
"buffering": {
3047+
"class": "logging.handlers.MemoryHandler",
3048+
"capacity": 5,
3049+
"target": "console",
3050+
"level": "DEBUG",
3051+
"flushLevel": "ERROR"
3052+
}
3053+
},
3054+
"loggers": {
3055+
"mymodule": {
3056+
"level": "DEBUG",
3057+
"handlers": ["buffering"],
3058+
"propagate": "true"
3059+
}
3060+
}
3061+
}
3062+
30393063
bad_format = {
30403064
"version": 1,
30413065
"formatters": {
@@ -3522,6 +3546,11 @@ def test_config17_ok(self):
35223546
h = logging._handlers['hand1']
35233547
self.assertEqual(h.formatter.custom_property, 'value')
35243548

3549+
def test_config18_ok(self):
3550+
self.apply_config(self.config18)
3551+
handler = logging.getLogger('mymodule').handlers[0]
3552+
self.assertEqual(handler.flushLevel, logging.ERROR)
3553+
35253554
def setup_via_listener(self, text, verify=None):
35263555
text = text.encode("utf-8")
35273556
# Ask for a randomly assigned port (by using port 0)

0 commit comments

Comments
 (0)