]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-117975: Ensure flush level is checked when configuring a logging MemoryHand...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 17 Apr 2024 13:48:09 +0000 (15:48 +0200)
committerGitHub <noreply@github.com>
Wed, 17 Apr 2024 13:48:09 +0000 (14:48 +0100)
(cherry picked from commit 6d0bb43232dd6ebc5245daa4fe29f07f815f0bad)

Lib/logging/config.py
Lib/test/test_logging.py

index 33417b75d519350a88ca2d76ea423907eecf12ed..2c0d30ccf806928e119110842d3e178ae414ca09 100644 (file)
@@ -768,18 +768,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:
index a4b2b4f9c8fd4018dab875289dfa29a29d5a6539..cf9fd427be2f912a2f75c598d01e0d6b1b64dec4 100644 (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 = {
         "version": 1,
         "formatters": {
@@ -3536,6 +3560,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)