Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8d27023

Browse files
committed
Allow configuration of handler properties.
1 parent 8f2b6ad commit 8d27023

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

Lib/logging/config.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ def configure_handler(self, config):
710710
'address' in config:
711711
config['address'] = self.as_tuple(config['address'])
712712
factory = klass
713+
props = config.pop('.', None)
713714
kwargs = dict([(k, config[k]) for k in config if valid_ident(k)])
714715
try:
715716
result = factory(**kwargs)
@@ -728,6 +729,9 @@ def configure_handler(self, config):
728729
result.setLevel(logging._checkLevel(level))
729730
if filters:
730731
self.add_filters(result, filters)
732+
if props:
733+
for name, value in props.items():
734+
setattr(result, name, value)
731735
return result
732736

733737
def add_handlers(self, logger, handlers):

Lib/test/test_logging.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,32 @@ class ConfigDictTest(BaseTest):
23892389
},
23902390
}
23912391

2392+
# As config0, but with properties
2393+
config14 = {
2394+
'version': 1,
2395+
'formatters': {
2396+
'form1' : {
2397+
'format' : '%(levelname)s ++ %(message)s',
2398+
},
2399+
},
2400+
'handlers' : {
2401+
'hand1' : {
2402+
'class' : 'logging.StreamHandler',
2403+
'formatter' : 'form1',
2404+
'level' : 'NOTSET',
2405+
'stream' : 'ext://sys.stdout',
2406+
'.': {
2407+
'foo': 'bar',
2408+
'terminator': '!\n',
2409+
}
2410+
},
2411+
},
2412+
'root' : {
2413+
'level' : 'WARNING',
2414+
'handlers' : ['hand1'],
2415+
},
2416+
}
2417+
23922418
def apply_config(self, conf):
23932419
logging.config.dictConfig(conf)
23942420

@@ -2625,6 +2651,15 @@ def test_config12_failure(self):
26252651
def test_config13_failure(self):
26262652
self.assertRaises(Exception, self.apply_config, self.config13)
26272653

2654+
def test_config14_ok(self):
2655+
with captured_stdout() as output:
2656+
self.apply_config(self.config14)
2657+
h = logging._handlers['hand1']
2658+
self.assertEqual(h.foo, 'bar')
2659+
self.assertEqual(h.terminator, '!\n')
2660+
logging.warning('Exclamation')
2661+
self.assertTrue(output.getvalue().endswith('Exclamation!\n'))
2662+
26282663
@unittest.skipUnless(threading, 'listen() needs threading to work')
26292664
def setup_via_listener(self, text, verify=None):
26302665
text = text.encode("utf-8")

0 commit comments

Comments
 (0)