0

I am trying to create a log config file that will create one complete log per day, but at the moment it creates multiple files;

ie. 
readings.log.2013-06-17_01
readings.log.2013-06-17_02
readings.log.2013-06-17_03
readings.log.2013-06-17_04
readings.log.2013-06-17_05
readings.log.2013-06-17_06
readings.log.2013-06-18_01
readings.log.2013-06-18_02
readings.log.2013-06-18_03
readings.log.2013-06-18_04
readings.log.2013-06-18_05
readings.log.2013-06-18_06
...etc

I'm sure I have missed something, but what do I need to change in my logging config file to make it create just one complete logfile PER day, regardless of size!>?

Using Python 2.7 atm and the script runs 24/7

Thx Matt.

My Logging config file; (logging_v3.cfg)

[loggers]
keys=root

[logger_root]
handlers=screen,file
level=NOTSET

[formatters]
keys=simple,complex,logtemps

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

[formatter_complex]
format=%(asctime)s - %(name)s - %(levelname)s - %(module)s : %(lineno)d - %(message)s

[formatter_logtemps]
format=%(asctime)s %(name)s %(levelname)s %(message)s

[handlers]
keys=file,screen

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
backupCount=0
formatter=logtemps
level=INFO
args=('logs/readings.log',)

[handler_screen]
class=StreamHandler
formatter=simple
level=DEBUG
args=(sys.stdout,)

And the code I have in my program to make the above work; (obviously there is a lot more but this is the main part for the logging)

import logging
import logging.config

logging.config.fileConfig('config/logging_v3.cfg') #logfile config

logging.debug("DEBUG MODE")
logging.debug("INFO MODE")
5
  • 1
    I'm not 100% sure, but I would try changing args=('logs/readings.log',) to args=('logs/readings.log','midnight',) - that looks like it'll pass the interval parameter as expected; I'm not sure what keys you can use in the logging config files, and it doesn't seem to be clearly documented as far as I can tell? Commented Jun 18, 2013 at 0:40
  • I think it's basically answered here: stackoverflow.com/questions/8467978/… Commented Jun 18, 2013 at 0:40
  • @9000, saw that one, but wasn't sure how to add it into a config file, like my setup. Commented Jun 18, 2013 at 1:26
  • @ernie, thx, works a lot better now. Another quick question, setting the backupcount=0, does that mean that it will not delete any of the logs? and likewise if I set it to 30, it will keep 30days of logs? Commented Jun 18, 2013 at 22:55
  • 1
    My reading of the doc suggests that's correct, i.e. "If backupCount is nonzero, at most backupCount files will be kept, and if more would be created when rollover occurs, the oldest one is deleted. The deletion logic uses the interval to determine which files to delete, so changing the interval may leave old files lying around." You may need to pass that in the args as well, e.g. args=('logs/readings.log', 'midnight', 'backupCount=0') or something similar. Commented Jun 18, 2013 at 23:13

1 Answer 1

2

Codifying the comments into an answer:

The documentation for the logging Configuration file format seems a little unclear as to what are valid options for keys and values in each section.

For the handlers, it appears to pass arguments to the constructors, we need to use the args key, and specify the values there, rather than using key-value pairs in the config section, e.g. instead of:

[handler_file]
class=handlers.TimedRotatingFileHandler
interval=midnight
args=('logs/readings.log',)

We should use:

[handler_file]
class=handlers.TimedRotatingFileHandler
args=('logs/readings.log', 'midnight',)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.