Logging with multiple loggers/handlers

Jeff Shannon jeff at ccvcorp.com
Thu Jul 8 01:35:59 CEST 2004


To follow up to my own post -- I've found my problem, and it appears to 
have been between my ears.  ;P

I somehow managed to misinterpret the spec for logging.getLogger().  
 From the current docs:

getLogger([name]):
    Return a logger with the specified name or, if no name is specified, 
return
    a logger which is the root logger of the hierarchy.

    All calls to this function with a given name return the same logger 
instance.
    This means that logger instances never need to be passed between 
different
    parts of an application.

Combined with this bit from PEP 282:


    Loggers are never instantiated directly. Instead, a module-level
    function is used:

    def getLogger(name=None): ...

    If no name is specified, the root logger is returned. Otherwise,
    if a logger with that name exists, it is returned. If not, a new
    logger is initialized and returned. Here, "name" is synonymous
    with "channel name".

And the descriptions of the config file
(http://www.red-dove.com/python_logging.html#config):

    #The channel value indicates the lowest portion of the channel name 
of the
    #logger. For a logger called "a.b.c", this value would be "c".

All of this combined to give me the impression that the name that's 
passed to getLogger() should be the "lowest portion" channel name, 
rather than the fully qualified name -- i.e., that I should be using 
"log03" instead of "log02.log03".  As it turns out, my impression was 
wrong -- it's the fully qualified name that should be used.  My attempts 
to use the unqualified name were creating new, unconfigured loggers 
(with no handlers attached) that could only propagate records up to the 
root logger.  I realized this only after inspecting the log manager's 
loggerDict and seeing the unconfigured "extra" loggers.

While it's fairly likely that my interpretation of the docs is not 
something that many people will come up with, I think it might be 
worthwhile to expand the example sections to actually show how to use 
multiple heirarchical loggers.  Had there been an example to follow, I 
certainly wouldn't have made this mistake.

Jeff Shannon
Technician/Programmer
Credit International





More information about the Python-list mailing list