strange behavious of the logging module?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sun Sep 23 18:24:45 EDT 2007


En Sun, 23 Sep 2007 18:53:53 -0300, Christian Meesters  
<meesters at uni-mainz.de> escribi�:

> import logging
> logging.basicConfig(level=logging.ERROR,
>                     format='%(levelname)-8s %(message)s',
>                     filename='mc_rigid.log',
>                     filemode='w')
> # define a Handler which writes INFO messages or higher to the sys.stderr
> console = logging.StreamHandler()
> console.setLevel(logging.INFO)
> # set a format which is simpler for console use
> formatter = logging.Formatter('%(message)s')
> # tell the handler to use this format
> console.setFormatter(formatter)
> # add the handler to the root logger
> logging.getLogger('').addHandler(console)
>
> I observe nothing printed on the console. However if I change the level
> (just after the import) from logging.ERROR to logging.DEBUG I do see all
> info lines on the console as intended.

basicConfig sets the root logger level; if it's ERROR, nothing below ERROR  
will pass. Your handler does not have a chance to get the message.

> Can anybody tell my mistake? I thought changing the level in basicConfig
> would have no impact on the following StreamHandler instance.

basicConfig sets the overall verbosity of your application.

> Also, adding %(funcName)-8s to the formatter in basigConfig does not work
> for me: instead of the functions name the level in lower case gets  
> inserted
> into the logfile.

Looks like a bug...

-- 
Gabriel Genellina




More information about the Python-list mailing list