]> granicus.if.org Git - python/commitdiff
Allow configuration of handler properties.
authorVinay Sajip <vinay_sajip@yahoo.co.uk>
Thu, 15 Nov 2012 14:20:18 +0000 (14:20 +0000)
committerVinay Sajip <vinay_sajip@yahoo.co.uk>
Thu, 15 Nov 2012 14:20:18 +0000 (14:20 +0000)
Lib/logging/config.py
Lib/test/test_logging.py

index 0694d21fe05e0c8d960efc3c3b7fa8cc90da0e99..ef7d2bcd451920df2c95dd81f7326dde6a9286d4 100644 (file)
@@ -710,6 +710,7 @@ class DictConfigurator(BaseConfigurator):
                 'address' in config:
                 config['address'] = self.as_tuple(config['address'])
             factory = klass
+        props = config.pop('.', None)
         kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
         try:
             result = factory(**kwargs)
@@ -728,6 +729,9 @@ class DictConfigurator(BaseConfigurator):
             result.setLevel(logging._checkLevel(level))
         if filters:
             self.add_filters(result, filters)
+        if props:
+            for name, value in props.items():
+                setattr(result, name, value)
         return result
 
     def add_handlers(self, logger, handlers):
index 92514533d959ea8f5b26fda9740948fd8b3aa700..54ab4862dde3299f3a74f38cb9e71487e0f4bbb0 100644 (file)
@@ -2389,6 +2389,32 @@ class ConfigDictTest(BaseTest):
         },
     }
 
+    # As config0, but with properties
+    config14 = {
+        'version': 1,
+        'formatters': {
+            'form1' : {
+                'format' : '%(levelname)s ++ %(message)s',
+            },
+        },
+        'handlers' : {
+            'hand1' : {
+                'class' : 'logging.StreamHandler',
+                'formatter' : 'form1',
+                'level' : 'NOTSET',
+                'stream'  : 'ext://sys.stdout',
+                '.': {
+                    'foo': 'bar',
+                    'terminator': '!\n',
+                }
+            },
+        },
+        'root' : {
+            'level' : 'WARNING',
+            'handlers' : ['hand1'],
+        },
+    }
+
     def apply_config(self, conf):
         logging.config.dictConfig(conf)
 
@@ -2625,6 +2651,15 @@ class ConfigDictTest(BaseTest):
     def test_config13_failure(self):
         self.assertRaises(Exception, self.apply_config, self.config13)
 
+    def test_config14_ok(self):
+        with captured_stdout() as output:
+            self.apply_config(self.config14)
+            h = logging._handlers['hand1']
+            self.assertEqual(h.foo, 'bar')
+            self.assertEqual(h.terminator, '!\n')
+            logging.warning('Exclamation')
+            self.assertTrue(output.getvalue().endswith('Exclamation!\n'))
+
     @unittest.skipUnless(threading, 'listen() needs threading to work')
     def setup_via_listener(self, text, verify=None):
         text = text.encode("utf-8")