8000 [3.12] gh-117975: Ensure flush level is checked when configuring a lo… · python/cpython@2b68c81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2b68c81

Browse files
[3.12] gh-117975: Ensure flush level is checked when configuring a logging MemoryHandler. (GH-117976) (GH-117986)
(cherry picked from commit 6d0bb43)
1 parent c270caf commit 2b68c81

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
@@ -768,18 +768,20 @@ def configure_handler(self, config):
768768
klass = cname
769769
else:
770770
klass = self.resolve(cname)
771-
if issubclass(klass, logging.handlers.MemoryHandler) and\
772-
'target' in config:
773-
# Special case for handler which refers to another handler
774-
try:
775-
tn = config['target']
776-
th = self.config['handlers'][tn]
777-
if not isinstance(th, logging.Handler):
778-
config.update(config_copy) # restore for deferred cfg
779-
raise TypeError('target not configured yet')
780-
config['target'] = th
781-
except Exception as e:
782-
raise ValueError('Unable to set target handler %r' % tn) from e
771+
if issubclass(klass, logging.handlers.MemoryHandler):
772+
if 'flushLevel' in config:
773+
config['flushLevel'] = logging._checkLevel(config['flushLevel'])
774+
if 'target' in config:
775+
# Special case for handler which refers to another handler
776+
try:
777+
tn = config['target']
778+
th = self.config['handlers'][tn]
779+
if not isinstance(th, logging.Handler):
780+
config.update(config_copy) # restore for deferred cfg
781+
raise TypeError('target not configured yet')
782+
config['target'] = th
783+
except Exception as e:
784+
raise ValueError('Unable to set target handler %r' % tn) from e
783785
elif issubclass(klass, logging.handlers.QueueHandler):
784786
# Another special case for handler which refers to other handlers
785787
# if 'handlers' not in config:

Lib/test/test_logging.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3050,6 +3050,30 @@ def format(self, record):
30503050
},
30513051
}
30523052

3053+
config18 = {
3054+
"version": 1,
3055+
"handlers": {
3056+
"console": {
3057+
"class": "logging.StreamHandler",
3058+
"level": "DEBUG",
3059+
},
3060+
"buffering": {
3061+
"class": "logging.handlers.MemoryHandler",
3062+
"capacity": 5,
3063+
"target": "console",
3064+
"level": "DEBUG",
3065+
"flushLevel": "ERROR"
3066+
}
3067+
},
3068+
"loggers": {
3069+
"mymodule": {
3070+
"level": "DEBUG",
3071+
"handlers": ["buffering"],
3072+
"propagate": "true"
3073+
}
3074+
}
3075+
}
3076+
30533077
bad_format = {
30543078
"version": 1,
30553079
"formatters": {
@@ -3536,6 +3560,11 @@ def test_config17_ok(self):
35363560
h = logging._handlers['hand1']
35373561
self.assertEqual(h.formatter.custom_property, 'value')
35383562

3563+
def test_config18_ok(self):
3564+
self.apply_config(self.config18)
3565+
handler = logging.getLogger('mymodule').handlers[0]
3566+
self.assertEqual(handler.flushLevel, logging.ERROR)
3567+
35393568
def setup_via_listener(self, text, verify=None):
35403569
text = text.encode("utf-8")
35413570
# Ask for a randomly assigned port (by using port 0)

0 commit comments

Comments
 (0)
0