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

Skip to content

Commit 3f885b5

Browse files
committed
Issue #17508: Handled out-of-order handler configuration correctly.
1 parent d5537d0 commit 3f885b5

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

Lib/logging/config.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2001-2010 by Vinay Sajip. All Rights Reserved.
1+
# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
22
#
33
# Permission to use, copy, modify, and distribute this software and its
44
# documentation for any purpose and without fee is hereby granted,
@@ -19,7 +19,7 @@
1919
is based on PEP 282 and comments thereto in comp.lang.python, and influenced
2020
by Apache's log4j system.
2121
22-
Copyright (C) 2001-2010 Vinay Sajip. All Rights Reserved.
22+
Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
2323
2424
To use, simply 'import logging' and log away!
2525
"""
@@ -564,14 +564,29 @@ def configure(self):
564564
# As handlers can refer to other handlers, sort the keys
565565
# to allow a deterministic order of configuration
566566
handlers = config.get('handlers', EMPTY_DICT)
567+
deferred = []
567568
for name in sorted(handlers):
569+
try:
570+
handler = self.configure_handler(handlers[name])
571+
handler.name = name
572+
handlers[name] = handler
573+
except Exception as e:
574+
if 'target not configured yet' in str(e):
575+
deferred.append(name)
576+
else:
577+
raise ValueError('Unable to configure handler '
578+
'%r: %s' % (name, e))
579+
580+
# Now do any that were deferred
581+
for name in deferred:
568582
try:
569583
handler = self.configure_handler(handlers[name])
570584
handler.name = name
571585
handlers[name] = handler
572586
except Exception as e:
573587
raise ValueError('Unable to configure handler '
574588
'%r: %s' % (name, e))
589+
575590
# Next, do loggers - they refer to handlers and filters
576591

577592
#we don't want to lose the existing loggers,
@@ -694,12 +709,17 @@ def configure_handler(self, config):
694709
c = self.resolve(c)
695710
factory = c
696711
else:
697-
klass = self.resolve(config.pop('class'))
712+
cname = config.pop('class')
713+
klass = self.resolve(cname)
698714
#Special case for handler which refers to another handler
699715
if issubclass(klass, logging.handlers.MemoryHandler) and\
700716
'target' in config:
701717
try:
702-
config['target'] = self.config['handlers'][config['target']]
718+
th = self.config['handlers'][config['target']]
719+
if not isinstance(th, logging.Handler):
720+
config['class'] = cname # restore for deferred configuration
721+
raise TypeError('target not configured yet')
722+
config['target'] = th
703723
except Exception as e:
704724
raise ValueError('Unable to set target handler '
705725
'%r: %s' % (config['target'], e))

Lib/test/test_logging.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
#
3-
# Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
3+
# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
44
#
55
# Permission to use, copy, modify, and distribute this software and its
66
# documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
1818

1919
"""Test harness for the logging module. Run all tests.
2020
21-
Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
21+
Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
2222
"""
2323

2424
import logging
@@ -1696,6 +1696,36 @@ class ConfigDictTest(BaseTest):
16961696
},
16971697
}
16981698

1699+
out_of_order = {
1700+
"version": 1,
1701+
"formatters": {
1702+
"mySimpleFormatter": {
1703+
"format": "%(asctime)s (%(name)s) %(levelname)s: %(message)s"
1704+
}
1705+
},
1706+
"handlers": {
1707+
"fileGlobal": {
1708+
"class": "logging.StreamHandler",
1709+
"level": "DEBUG",
1710+
"formatter": "mySimpleFormatter"
1711+
},
1712+
"bufferGlobal": {
1713+
"class": "logging.handlers.MemoryHandler",
1714+
"capacity": 5,
1715+
"formatter": "mySimpleFormatter",
1716+
"target": "fileGlobal",
1717+
"level": "DEBUG"
1718+
}
1719+
},
1720+
"loggers": {
1721+
"mymodule": {
1722+
"level": "DEBUG",
1723+
"handlers": ["bufferGlobal"],
1724+
"propagate": "true"
1725+
}
1726+
}
1727+
}
1728+
16991729
def apply_config(self, conf):
17001730
logging.config.dictConfig(conf)
17011731

@@ -1994,6 +2024,10 @@ def test_listen_config_1_ok(self):
19942024
# Original logger output is empty.
19952025
self.assert_log_lines([])
19962026

2027+
def test_out_of_order(self):
2028+
self.apply_config(self.out_of_order)
2029+
handler = logging.getLogger('mymodule').handlers[0]
2030+
self.assertIsInstance(handler.target, logging.Handler)
19972031

19982032
class ManagerTest(BaseTest):
19992033
def test_manager_loggerclass(self):

0 commit comments

Comments
 (0)