Any logger created before calling logging.config.dictCOnfig is notconfigured

  • Thread starter W. Matthew Wilson
  • Start date
W

W. Matthew Wilson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top