Vivek S
Vivek S

Reputation: 5540

django 1.3 logging timedrotatingfilehandler truncating file on every write

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?

Upvotes: 3

Views: 2819

Answers (3)

Fish Monitor
Fish Monitor

Reputation: 4535

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

Upvotes: 0

pass-by-ref
pass-by-ref

Reputation: 1298

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.

Upvotes: 0

zvikico
zvikico

Reputation: 9825

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).

Upvotes: 1

Related Questions