logging issues

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Dec 13 11:11:31 EST 2011


Andrea Crotti wrote:
> I think is simple but I can't get it to work as I wish.
> Suppose I have a big application, my idea is that the running
> script sets a global logging level and then all the imported modules
> would act consequently.
>
> In my codebase, however, unless I set the level for each of the loggers
> I don't get the debug messages, not really sure why yet.
>
> To check if I understood,
> logging.getLogger() returns the root logger, and every other logger
> should inherit its settings, is that correct?
Not really, if you use the classic way, your subloggers should be 
created an never configured. Their default behavior is to raise any log 
event to its parent. That way, all event logs end up being handled by 
the root logger.
>
> What happens if there is another logging.getLogger() somewhere else
> in the code after the initialization? Does that become the root logger
> overriding the old one?
multiple calls to logging.getLogger() always return the very same root 
logger (the root one). Loggers are static objects handled by the loggin 
module, once a logger is created (identified by its name, the root 
logger's name is "") it will never be destroyed. Multiple call the 
getLogger(name) with the same name returns the same object.

Logger names are of the form "grandparent.parent.child"
>
> The example below instead works as expected, so maybe is something
> with my codebase...
your codebase which is ???

[snip some working code]

As a general advices
- try to never configure a child logger, you should only need to create 
one whithout setting its level nor its handler. A logger is responsible 
of raising log events, not handling them. The root logger is supposed to 
handle them.
- Configure only the root logger and do it in the main function, and 
only there. Configuring means calling basicConfig, setting the level, 
adding a handler etc... everything except the creation.

JM



More information about the Python-list mailing list