Why do all my loggers start auto-disabled?

jonfroehlich jonfroehlich at gmail.com
Mon Aug 25 12:23:42 EDT 2008


I am a relative newbie to Python and its logging infrastructure;
however, I have programmed extensively with Java/C# and log4j and
log4net. That, I suppose, could be the root of my problem :)

I am trying to setup one logger per module (as this is roughly
analogous to how I've used log4j/log4net). However, I've noticed that
my loggers have the disabled flag set to true (more accurately, 1)
when they are initialized. For each logger, do I have to force
disabled = False? To make this more concrete, here is an example.

In example.py:

import logging
import logging.config

_log = logging.getLogger("prediction.predict")

def main():
    logging.config.fileConfig('logconfig.conf')
    _log.warn("test warning!")

if __name__ == "__main__":
    main()

Unfortunately, this does not print out "test warning!" and if I put a
breakpoint at the _log.warn("test warning!") line, _log.disabled
equals 1. If I  change main to:

def main():
    logging.config.fileConfig('predict_logconfig.conf')
    _log.disabled = False
    _log.warn("test warning!")

It works! So, am I doing something wrong or do I always have to force
my loggers to be enabled? Should I update my configuration file so
that loggers start as enabled? Here is my config file
(logconfig.conf):

[formatters]
keys: detailed,simple

[handlers]
keys: console,filelog

[loggers]
keys: root

[formatter_simple]
format: %(levelname)s : %(module)s.%(funcName)s (%(lineno)d): %
(message)s

[formatter_detailed]
format: %(asctime)s %(levelname)s : %(module)s.%(funcName)s (%
(lineno)d): %(message)s

[handler_console]
class: StreamHandler
args: []
formatter: simple

[handler_filelog]
class: FileHandler
args: ["predictionLog.txt"]
formatter: detailed

[logger_root]
level: INFO
handlers: filelog, console






More information about the Python-list mailing list