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

Discussion in 'Python' started by W. Matthew Wilson, Mar 6, 2013.

  1. 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 code

    and then

    $ python dict

    and see how the logging output is different.



    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}


    def configure_logging_with_code():

    # Set the logger to 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()

    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':

    elif args.how_to_configure == 'dict':

    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, Mar 6, 2013
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.