gh-117975: Ensure flush level is checked when configuring a logging MemoryHandler. (GH-117976)

This commit is contained in:
Vinay Sajip 2024-04-17 13:55:18 +01:00 committed by GitHub
parent b9b3c455f0
commit 6d0bb43232
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 43 additions and 12 deletions

View File

@ -761,18 +761,20 @@ class DictConfigurator(BaseConfigurator):
klass = cname
else:
klass = self.resolve(cname)
if issubclass(klass, logging.handlers.MemoryHandler) and\
'target' in config:
# Special case for handler which refers to another handler
try:
tn = config['target']
th = self.config['handlers'][tn]
if not isinstance(th, logging.Handler):
config.update(config_copy) # restore for deferred cfg
raise TypeError('target not configured yet')
config['target'] = th
except Exception as e:
raise ValueError('Unable to set target handler %r' % tn) from e
if issubclass(klass, logging.handlers.MemoryHandler):
if 'flushLevel' in config:
config['flushLevel'] = logging._checkLevel(config['flushLevel'])
if 'target' in config:
# Special case for handler which refers to another handler
try:
tn = config['target']
th = self.config['handlers'][tn]
if not isinstance(th, logging.Handler):
config.update(config_copy) # restore for deferred cfg
raise TypeError('target not configured yet')
config['target'] = th
except Exception as e:
raise ValueError('Unable to set target handler %r' % tn) from e
elif issubclass(klass, logging.handlers.QueueHandler):
# Another special case for handler which refers to other handlers
# if 'handlers' not in config:

View File

@ -3036,6 +3036,30 @@ class ConfigDictTest(BaseTest):
},
}
config18 = {
"version": 1,
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
},
"buffering": {
"class": "logging.handlers.MemoryHandler",
"capacity": 5,
"target": "console",
"level": "DEBUG",
"flushLevel": "ERROR"
}
},
"loggers": {
"mymodule": {
"level": "DEBUG",
"handlers": ["buffering"],
"propagate": "true"
}
}
}
bad_format = {
"version": 1,
"formatters": {
@ -3522,6 +3546,11 @@ class ConfigDictTest(BaseTest):
h = logging._handlers['hand1']
self.assertEqual(h.formatter.custom_property, 'value')
def test_config18_ok(self):
self.apply_config(self.config18)
handler = logging.getLogger('mymodule').handlers[0]
self.assertEqual(handler.flushLevel, logging.ERROR)
def setup_via_listener(self, text, verify=None):
text = text.encode("utf-8")
# Ask for a randomly assigned port (by using port 0)