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

Skip to content

Commit 5a63fe6

Browse files
committed
Closes #17508: Merged fix from 3.3.
2 parents 671ddbe + 340a4bb commit 5a63fe6

2 files changed

Lines changed: 61 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-2012 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-2012 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: 37 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
@@ -2415,6 +2415,36 @@ class ConfigDictTest(BaseTest):
24152415
},
24162416
}
24172417

2418+
out_of_order = {
2419+
"version": 1,
2420+
"formatters": {
2421+
"mySimpleFormatter": {
2422+
"format": "%(asctime)s (%(name)s) %(levelname)s: %(message)s"
2423+
}
2424+
},
2425+
"handlers": {
2426+
"fileGlobal": {
2427+
"class": "logging.StreamHandler",
2428+
"level": "DEBUG",
2429+
"formatter": "mySimpleFormatter"
2430+
},
2431+
"bufferGlobal": {
2432+
"class": "logging.handlers.MemoryHandler",
2433+
"capacity": 5,
2434+
"formatter": "mySimpleFormatter",
2435+
"target": "fileGlobal",
2436+
"level": "DEBUG"
2437+
}
2438+
},
2439+
"loggers": {
2440+
"mymodule": {
2441+
"level": "DEBUG",
2442+
"handlers": ["bufferGlobal"],
2443+
"propagate": "true"
2444+
}
2445+
}
2446+
}
2447+
24182448
def apply_config(self, conf):
24192449
logging.config.dictConfig(conf)
24202450

@@ -2787,6 +2817,11 @@ def verify_reverse(stuff):
27872817
('ERROR', '2'),
27882818
], pat=r"^[\w.]+ -> (\w+): (\d+)$")
27892819

2820+
def test_out_of_order(self):
2821+
self.apply_config(self.out_of_order)
2822+
handler = logging.getLogger('mymodule').handlers[0]
2823+
self.assertIsInstance(handler.target, logging.Handler)
2824+
27902825
def test_baseconfig(self):
27912826
d = {
27922827
'atuple': (1, 2, 3),

0 commit comments

Comments
 (0)