3
votes

I am using django 1.3's logging feature and trying to implement a timedrotatingfilehandler to rotate logs every hour.The logger is rotating successfully after every hour but it seems during every log request it truncates the file.The file has only the last written message.Is this an issue in django handler or am i missing somewhere.The logging dictionary is below:

LOGGING = {
'version': 1,
'disable_existing_loggers': True,
'formatters': {
    'standard': {
        'format' : "%(asctime)s:%(pathname)s:%(lineno)s: %(message)s",
        'datefmt' : "%d/%b/%Y %H:%M:%S"
    },
},
'handlers': {
    'logfile': {
        'level':'DEBUG',
        'class':'logging.handlers.TimedRotatingFileHandler',
        'filename': "/tmp/log1.log",
    'when' : 'hour',
    'interval' : 0,
        'formatter': 'standard',
    },
},
'loggers': {
    'collection': {
        'handlers': ['logfile'],
        'level': 'DEBUG',
    },
}
}

Please note: when the interval is set to 1, the log is not getting rotated.Is this a bug in django?

3

3 Answers

1
votes

You need to set:

'when' : 'H',
'interval' : 1,

From the code, current 'when' events supported:

  • S - Seconds
  • M - Minutes
  • H - Hours
  • D - Days
  • midnight - roll over at midnight
  • W{0-6} - roll over on a certain day; 0 - Monday

Interval is the number of intervals to count (e.g. when == 'H' and interval == 2 will result in 2 hours).

0
votes

Whenever you are creating a log file, just add a datetime stamp to the filename. This will make sure that the file will never be truncated.

0
votes

I guess there are multiple processes writing to your log file, in which case you can use ConcurrentLogHandler to avoid truncating.