logging.config.fileConfig disables existing loggers

Discussion in 'Python' started by Robert, Aug 30, 2007.

  1. Robert

    Robert Guest

    Hi

    I have been grappling today with some unexpected behaviour from
    logging.config.fileConfig.

    I am using the following config file:
    [loggers]
    keys=root

    [handlers]
    keys=console

    [formatters]
    keys=form01

    [logger_root]
    level=DEBUG
    handlers=console

    [handler_console]
    class=StreamHandler
    level=NOTSET
    formatter=form01
    args=(sys.stdout,)

    [formatter_form01]
    format="%(asctime)s - %(name)s - %(filename)s:%(lineno)s - %
    (levelname)s - %(message)s"
    datefmt=

    As you can see, it sets the root logger level to be DEBUG. I would
    expect this to have the same effect as calling logger.basicConfig with
    a level = DEBUG. What instead happens is that all my loggers become
    disabled. One might argue that the loggers should only be created
    after configuring the logger, but what about using the listening
    method to configure again later...

    While I can understand that the application of a new config file is
    not incremental, ie should override any previous configuration
    completely, it does not appear right that only loggers explicitly
    named in the config file are enabled. I would expect that if I enabled
    a particular logger then all child loggers should be enabled too.
    Alternatively one needs to know the names of all loggers a-priori in
    order to be able to monitor them.

    So I took the liberty to try to discern what is going on. The culprit
    (from my perspective) was the logging.config._install_loggers method,
    particularly the last two lines which disable any logger not mentioned
    in the config file. If those lines are deleted and a new loop is
    created at the top of the function which resets all existing loggers
    to their default status, then we see the desired behaviour. This seems
    quite simple and sensible. Below is a patch file for applying the
    change. What do people think?

    Robert

    --- config.py.old 2007-08-30 13:42:39.000000000 +0200
    +++ config.py 2007-08-30 13:53:15.000000000 +0200
    @@ -173,6 +173,14 @@
    def _install_loggers(cp, handlers):
    """Create and install loggers"""

    + # reset all existing loggers so that they can inherit new
    configuration
    + for log in logging.root.manager.loggerDict.keys():
    + logger = logging.root.manager.loggerDict[log]
    + logger.level = logging.NOTSET
    + logger.propagate = 1
    + logger.handlers = []
    + logger.disabled = 0
    +
    # configure the root first
    llist = cp.get("loggers", "keys")
    llist = string.split(llist, ",")
    @@ -192,16 +200,6 @@
    for hand in hlist:
    log.addHandler(handlers[hand])

    - #and now the others...
    - #we don't want to lose the existing loggers,
    - #since other threads may have pointers to them.
    - #existing is set to contain all existing loggers,
    - #and as we go through the new configuration we
    - #remove any which are configured. At the end,
    - #what's left in existing is the set of loggers
    - #which were in the previous configuration but
    - #which are not in the new configuration.
    - existing = root.manager.loggerDict.keys()
    #now set up the new ones...
    for log in llist:
    sectname = "logger_%s" % log
    @@ -212,8 +210,6 @@
    else:
    propagate = 1
    logger = logging.getLogger(qn)
    - if qn in existing:
    - existing.remove(qn)
    if "level" in opts:
    level = cp.get(sectname, "level")
    logger.setLevel(logging._levelNames[level])
    @@ -227,12 +223,6 @@
    for hand in hlist:
    logger.addHandler(handlers[hand])

    - #Disable any old loggers. There's no point deleting
    - #them as other threads may continue to hold references
    - #and by disabling them, you stop them doing any logging.
    - for log in existing:
    - root.manager.loggerDict[log].disabled = 1
    -

    def listen(port=DEFAULT_LOGGING_CONFIG_PORT):
    """
     
    Robert, Aug 30, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Almad
    Replies:
    1
    Views:
    327
    Vinay Sajip
    Oct 7, 2006
  2. Lowell Alleman
    Replies:
    3
    Views:
    554
    Vinay Sajip
    May 29, 2008
  3. Alexandru  Moșoi

    how to use logging.config.fileConfig ?

    Alexandru Moșoi, Apr 10, 2009, in forum: Python
    Replies:
    1
    Views:
    716
    Vinay Sajip
    Apr 11, 2009
  4. Rhino
    Replies:
    8
    Views:
    537
    markspace
    Apr 30, 2010
  5. Herman
    Replies:
    0
    Views:
    196
    Herman
    Feb 19, 2012
Loading...

Share This Page