Issue 921318: Patch for Vinay Sajip's Python-logging package (original) (raw)

This is a patch for Vinay Sajip's Python-logging package. It adds the support for time-based log rollovers. Specifically, it adds a new handler called TimedRotatingFIleHandler. TimedRotatingFileHandler works just like the existing RotatingFileHandler, except that instead of rolling over when the log file hits a certain size, it rolls over when a time-interval has occurred.

You can have the log files rollover ever seconds, minutes, hours, days. You can have log files rollover at midnight. Log files can also roll over on a day of the week.

The name of the log file is customized to fit the interval specified. In other words, it's suffix is changed to match the interval you asked for. If you asked for it to roll over every hour, then the log file has a suffix with the year, month, day and hour, but if you roll over on a certain minute, then the suffix also has the minute.

I had an extended correspondence with Vinay about this code as I was writing it, so he knows it's coming.

It's been mostly tested, but not every variation has been exercised. The rollovers at seconds, minutes, hours have been exercised, as well as the midnight rollover, but the not the day of week. Additionally, the backupCount variation of the TimedRotatingFileHandler is somewhat simplistic and not well tested.

This patch only affects handlers.py. It does NOT patch the graphical TkInter-based log configuration utility, as I ran out of time to work on this and Vinay said he might change the class layout.

Logged In: YES user_id=764593

(1) I think the H in the suffixes need to be capitalized.

time.strftime("%Y-%m-%d_%h-%M-%S", tt) '2004-03-25_-53-07' time.strftime("%Y-%m-%d_%H-%M-%S", tt) '2004-03-25_10-53-07'

(2)
When globbing for old logfiles to delete, please make it more specfic. For instance, all suffixes (for the next century) start with "20", so you could use

s = glob.glob(self.basFilename + ".20*")  

This will make it a bit less dangerous for someone to copy a file as basename.save when they want to save the info for debugging later.

(3)
The midnight and weekly versions should recalculate the rolloverAt instead of adding interval. This will keep them from getting out of synch if there is a maintenance window or daylight savings time change.