GPX
GPX

Reputation: 3645

Writing a custom RotatingFileHandler that rotates logs every day

I am developing a Django application, and would like a custom handler that rotates the log everyday - I've struggled with the TimedRotatingFileHandler (the infamous 'midnight' confusion).

The handler needs to rotate the log file based on the current system date - i.e., a new log file for each day. How do I do this?

Upvotes: 0

Views: 2232

Answers (2)

GPX
GPX

Reputation: 3645

I think the best solution for me would be to just build the log file name with the date. I've done this.

'handlers': {
                'default': {
                           'level':'DEBUG',
                           'class':'logging.FileHandler',
                           'filename': '/path/debug_' + date.today().strftime('%d%m%Y') + '.log',
                           'formatter':'standard',
                           },
}

Upvotes: 3

Vinay Sajip
Vinay Sajip

Reputation: 99317

If you are on a POSIX platform using Python >= 2.6 you could use an external log rotator (e.g. logrotate) together with a WatchedFileHandler.

Upvotes: 1

Related Questions