Mysterious Logging Handlers
Peter Otten
__peter__ at web.de
Fri Sep 9 14:30:36 EDT 2016
Josh English wrote:
> I have a Python script that imports a utility script. Both scripts use
> logging, but the logs don't work as advertised. I'm getting logging output
> from the utility script but none from the main file. Worse, the format of
> the utility script's logs don't match anything I define.
>
> The utility script is called xlreader.py. It has the following lines:
>
> -- begin snippet --
> import logging
> XLOGGER = logging.getLogger('XLREADER')
> -- end snippet --
>
> And it calls XLOGGER.debug(), XLOGGER.error() and XLOGGER.info() but it
> never defines a handler.
>
>
> The main script imports xlreader but does not reference XLOGGER. It
> defines its own logger:
>
> -- begin snippet --
> import logging
> import xlreader
>
> LOG = logging.getLogger('SHIPPING')
> FORMAT = '%(asctime)-15s %(name)s %(level)-8s %(message)s'
That should be either levelname or levelno in the format string.
> logging.basicConfig(format=FORMAT,level=logging.DEBUG)
> handler = logging.StreamHandler()
> formatter = logging.Formatter(FORMAT)
> handler.setFormatter(formatter)
> LOG.addHandler(handler)
> -- end snippet --
>
> I added the logging.basicConfig line but it didn't have any effect.
That means there are already handlers configured for the root logger, for
example because basicConfig() has been called before.
> I
> created the second handler separately.
>
> When I run the scriptI get logging information from only xlreader, not
> from the main script:
>
> DEBUG:XLREADER:Creating Excel Reader
>
> This format isn't defined anywhere. T
>
> Even more mysterious, after I run the file (in an IDE so I have a REPL
> afterwards), I have:
Don't run your code in an IDE. The interaction between your and their code
can make debugging harder than necessary.
More information about the Python-list
mailing list