Getting a TimedRotatingFileHandler not to put two dates in the same file?
vinay_sajip at yahoo.co.uk
Wed Oct 24 17:48:53 CEST 2012
David M Chess <chess <at> us.ibm.com> writes:
> >> But now the users have noticed that if the process isn't up at
> >> they can end up with lines from two (or I guess potentially more)
> dates in
> >> the same log file.
> >> Is there some way to fix this, either with cleverer arguments
> into the
> >> TimedRotatingFileHandler, or by some plausible subclassing of
> it or its
> >> superclass?
Well, of course you can subclass and override it to do what you want - there's
no magic there. The behaviour is as you would expect: the default behaviour of
a TimedRotatingFileHandler is to append, and roll over at midnight. So if your
program isn't running at midnight, it won't rotate:
Day 1. Run your program, stop it before midnight. The log file contains dates
from this day. No rotation occurred.
Day 2. Run your program again, stop it before midnight. The log file contains
dates from this day, and Day 1. No rotation occurred.
That's the symptom you're seeing, right?
You could do as Dave Angel suggested - just use a FileHandler with the name
derived from the date. If you don't want to or can't actually rotate files at
midnight, you're using the wrong tool for the job :-)
If you sometimes want to rotate at midnight (the process is running at that
time) and at other times not (the process isn't running then), you might have
to code startup logic in your program to deal with the vagaries of your
environment, since only you would know what they are :-)
Work is afoot to make the actual rollover time configurable (i.e. not forced
to be literally midnight) - see http://bugs.python.org/issue9556 - but that's
an enhancement request, not a bug, and so it'll see the light of day in Python
3.4, if at all. An implementation is in my sandbox repo at
in branch fix9556. If all you need to do is rollover at a different time daily
(say 7 a.m.), you might be able to use this. Feel free to use that code as
inspiration for your subclass.
More information about the Python-list