Issue 2317: TimedRotatingFileHandler logic for removing files wrong (original) (raw)
There are three issues with log file removal in the TimedRotatingFileHandler class:
Removal will stop working in the year 2100, as the code assumes that timestamps start with ".20".
If you run an application with backupCount set to a high number, and then restarts it with a lower number, the code will still not remove as many log files as you expect. It will never remove more than one file when it rotates the log.
It assumes that no other files matches baseFilename + ".20*", so make sure that you don't log to both "log" and "log.20th.century.fox" in the same directory!
Suggested fix: use os.listdir() instead of glob.glob(), filter all file names using a proper regexp, sort the result, and use a while loop to remove files until the result is small enough. To reduce the risk of accidentally removing an unrelated file, the filter regexp should be based on the logging interval, just as the filename is.
My suggested fix means that old files may not be removed if you change the interval. I think that is an acceptable behavior, but it should probably be documented to avoid future bug reports on this subject. :-)