(beginner) logging config not working

Unknown Moss unknownmoss at gmail.com
Fri Apr 29 20:17:56 EDT 2011


Hi

This is a beginner question. Thanks for the hand.

I've been asked to maintain some poorly constructed python code.
Logging is weak. Getting it to work with python logging
programmatically was easy.

However, I'd like to refactor all the logging code into configuration
since I see the need for other handlers in the future (syslog and
possibly email).

For now I just want to get console and log file working, but I'm
having problems with the log file. I'm trying some test code. Here's
my test script (logging_example.py):

import logging
import logging.config

def main():
    logging.config.fileConfig("logging.conf")
    logging.debug("debug check")
    logging.info("info check")
    logging.warn("warn check")
    logging.error("err check")
    logging.critical("crit check")

if __name__ == "__main__":
  main()

Here's my config (logging.conf):

[loggers]
keys=root,file

[handlers]
keys=console,file

[formatters]
keys=simple,detailed

[logger_root]
level=NOTSET
handlers=console

[logger_file]
level=DEBUG
handlers=file
qualname=mylogger

[formatter_simple]
class=logging.Formatter
format=%(asctime)s - %(name)s [%(levelname)s] - %(message)s

[formatter_detailed]
class=logging.Formatter
format=%(asctime)s - %(name)s [%(levelname)s] - %(message)s

[handler_console]
class=logging.StreamHandler
formatter=simple
args=(sys.stdout,)

[handler_file]
class=FileHandler
level=DEBUG
formatter=detailed
args=('logging_example.log', 'w')


Output:

$ python logging_example.py
2011-04-29 17:07:01,923 - root [DEBUG] - debug check
2011-04-29 17:07:01,986 - root [INFO] - info check
2011-04-29 17:07:01,986 - root [WARNING] - warn check
2011-04-29 17:07:01,986 - root [ERROR] - err check
2011-04-29 17:07:02,000 - root [CRITICAL] - crit check

The logging_example.log is created, but no entries are written to it.
Based on this configuration I'd expect all the logging entries to be
written to the file as well.

Any ideas where I'm going awry?



More information about the Python-list mailing list