Specifying two log files with one configuration file

P

Peter Steele

I want to configure the Python logging module to manage two separate log files, allowing me to do something like this:

import logging
import logging.config
logging.config.fileConfig("mylogging.conf")
root = logging.getLogger()
test = logging.getLogger("test")

root.debug("This is a message targeted to the root log file")
test.debug("This is a message targeted to the test log file")

I have been unable to get this to work. My current conf file looks like this:

[formatters]
keys: simple

[handlers]
keys: root,test

[loggers]
keys: root,test

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

[handler_root]
class: handlers.RotatingFileHandler
args: ('/var/log/root.log', 'a', 1024000, 14)
formatter: simple

[handler_test]
class: handlers.RotatingFileHandler
args: ('/var/log/test.log', 'a', 1024000, 14)
formatter: simple

[logger_root]
level: DEBUG
handlers: root
qualname:

[logger_test]
level: DEBUG
handlers: test
qualname:

With this setup, all of my log messages go to test.log. root.log is created, but nothing gets logged to it. What do I need in my logging conf file to have two separate log file destinations?
 
V

Vinay Sajip

Peter Steele said:
I have been unable to get this to work. My current conf file looks like this:

Try with the following changes:

[logger_test]
level: DEBUG
handlers: test
propagate: 0
qualname: test

The qualname: test is what identifies the logger as the logger named 'test', and
propagate: 0 prevents the test message from being passed up to the root logger.

Regards,

Vinay Sajip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top