Logging oddity: handlers mandatory in every single logger?

Jean-Michel Pichavant jeanmichel at sequans.com
Tue Feb 2 11:52:48 EST 2010


Masklinn wrote:
> Jean-Michel Pichavant wrote:
>   
>> To add a custom level, I would proceed that way:
>>
>> logging.ALERT = 45
>> logging.addLevelName(logging.ALERT, 'ALERT !!')
>> logging.getLogger().log(logging.ALERT, 'test')
>>
>> Passing a string to the log method as you did is incorrect.
>>     
>
> I know it's currently incorrect. My point was more along the line that there was *no reason* for it to be incorrect. logging already contains all the tools for log('PANTS_ON_FIRE') to be allowed
>   
The reason is that log takes an *int* as first argument that defines the 
logging level. You gave a string. So There is definitely a reason for it 
to be incorrect.
>   
>> Regarding your first point, I guess it's anti pattern. One way to do it:
>> 1/ Configure the root logger with the lowest value 0, so the root logger 
>> does not filter any level.
>> 2/ Configure each of your logger with the correct level
>>
>> That way you can configure your '0' logger as you (badly :o)) named it 
>> with one level, and configure a potential '1' logger with another level. 
>> Don't bother with propagation. That way you won't need to duplicate your 
>> handlers on every logger.
>>     
>
> re logger 0, no need for complex name for a test case (and it allowed me to create easy-to-remember 0.1 and 0.1.2 if needed)
>
> Re your answer, from what I understand you want the root logger to NOTSET and then each child logger with its correct level? But that's not a solution, each and every level will *still* require a handler explicitly configured on it. That's in fact very much my issue: logging refuses that a logger be handler-less in a config file, it's mandatory to configure a handler any time a logger is configured.
>   
the field handlers must be defined even if empty.

[loggers]
keys=root,0,1

[handlers]
keys=console

[formatters]
keys=simple

[logger_root]
level=DEBUG
handlers=console

[logger_1]
level=INFO
qualname=1
handlers=

[logger_0]
level=DEBUG
qualname=0
handlers=

[handler_console]
class=StreamHandler
formatter=simple
args=()

[formatter_simple]
format=%(asctime)s:%(levelname)-8s:%(name)s::%(message)s


import logging; import logging.config
logging.config.fileConfig("log.config")
l1 = logging.getLogger("1")
l0 = logging.getLogger("0")
l1.debug('I am l1')
...
l0.debug('I am l0')
... 2010-02-02 17:48:55,710:DEBUG   :0::I am l0

JM



More information about the Python-list mailing list