Any logger created before calling logging.config.dictCOnfig is not configured
W. Matthew Wilson
matt at tplus1.com
Wed Mar 6 12:53:38 EST 2013
It seems like that any logger I create BEFORE calling
logging.config.dictConfig does not get configured.
Meanwhile, if I configure the logger like I always have, by just setting
handlers on root, everything works fine, including the loggers that were
created BEFORE I configure logging.
I make a lot of module-level log instances.
I wrote a simple script to show my problem. Run it like:
$ python scratch.py code
and then
$ python scratch.py dict
and see how the logging output is different.
### SCRIPT START
"""
import argparse
import logging
import logging.config
log1 = logging.getLogger('scratch')
def configure_logging_with_dictConfig():
d = {
'formatters': {
'consolefmt': {
'format': '%(asctime)s %(levelname)-10s %(process)-6d
%(name)-24s %(lineno)-4d %(message)s'}},
'handlers': {
'console': {
'class': 'logging.StreamHandler',
'formatter': 'consolefmt',
'level': 'DEBUG'}},
'root': {
'handlers': ['console'],
'level': 'DEBUG'},
'version': 1}
logging.config.dictConfig(d)
def configure_logging_with_code():
# Set the logger to DEBUG.
logging.root.setLevel(logging.DEBUG)
# Now write a custom formatter, so that we get all those different
# things.
f = logging.Formatter(
'%(asctime)s '
'%(levelname)-10s '
'%(process)-6d '
'%(filename)-24s '
'%(lineno)-4d '
'%(message)s '
)
# Set up a stream handler for DEBUG stuff (and greater).
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(f)
logging.root.addHandler(sh)
def set_up_arguments():
ap = argparse.ArgumentParser()
ap.add_argument('how_to_configure', choices=['code', 'dict'])
return ap.parse_args()
if __name__ == '__main__':
args = set_up_arguments()
if args.how_to_configure == 'code':
configure_logging_with_code()
elif args.how_to_configure == 'dict':
configure_logging_with_dictConfig()
log1.debug('debug from log1')
# log2 is created AFTER I configure logging.
log2 = logging.getLogger('log2')
log2.debug('debug from log2')
# Try to figure out what is the difference! Nothing jumps out at me.
print "log.root.level: {0}".format(log1.root.level)
print "log.root.handlers: {0}".format(log1.root.handlers)
print "log1.parent.level: {0}".format(log1.parent.level)
print "log1.parent.handlers: {0}".format(log1.parent.handlers)
print "log1.level: {0}".format(log1.level)
print "log1.handlers: {0}".format(log1.handlers)
print "log1.propagate: {0}".format(log1.propagate)
print "log1.getEffectiveLevel(): {0}".format(log1.getEffectiveLevel())
### SCRIPT END
--
W. Matthew Wilson
matt at tplus1.com
http://tplus1.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130306/7851a184/attachment.html>
More information about the Python-list
mailing list