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

(cherry picked from commit 6d0bb43232)
This commit is contained in:
Miss Islington (bot) 2024-04-17 15:48:09 +02:00 committed by GitHub
parent c270caf966
commit 2b68c81283
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 43 additions and 12 deletions

View file

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

View file

@ -3050,6 +3050,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 = { bad_format = {
"version": 1, "version": 1,
"formatters": { "formatters": {
@ -3536,6 +3560,11 @@ class ConfigDictTest(BaseTest):
h = logging._handlers['hand1'] h = logging._handlers['hand1']
self.assertEqual(h.formatter.custom_property, 'value') 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): def setup_via_listener(self, text, verify=None):
text = text.encode("utf-8") text = text.encode("utf-8")
# Ask for a randomly assigned port (by using port 0) # Ask for a randomly assigned port (by using port 0)