First, a note: it's generally unwise to adjust the .suffix
field of a RotatingFileHandler
as there are actually two fields that depend on the log file name suffix. One is a literal suffix represented as a strftime
format directive, and the other is a regular expression that is used to choose old log files to remove. If you set one without setting the other, you can make your code fail to remove files (this is the most likely), or to remove the wrong files (much less likely). Fortunately the .suffix
you set is the .suffix
already set by using when='midnight'
.
Next, the design of these rotating file handlers is not what you are expecting. Instead, there is a notion of a current file, and then some number of saved backup files. The current file has no suffix: it's just named mylog
. This is always the case.
Every once in a while—at the rotation interval-and-time, or some time after it has passed if you are logging infrequently enough—the logging module will notice that, oh look at the time/date/whatever, it's time for a new mylog
file. At this point it:
- renames the existing
mylog
file, if there is one, to a backup name using the suffix it chose based on your when
argument;
- creates a new
mylog
(unsuffixed) file; and
- removes any extra backup files, if there are now "too many".
In other words, at or sometime after midnight—this is your chosen "when"; the actual sequence of events occurs only upon logging a message—your existing handler notices that it's time for a new file. It looks for mylog.<some-suffix>
file names where the suffix part matches:
r"^\d{4}-\d{2}-\d{2}$"
(this is hardcoded for midnight
; there are other hardcoded regular expressions for other when
values). If there are fewer than backupCount
(10) such files now, the existing ones remain in place. Otherwise enough of the oldest1 of those files are removed so that there are only 9 remaining. The existing mylog
then becomes mylog.2017-03-13
or similar (depending on the date of course), and a new mylog
-with-no-suffix is opened to be the current log file.
If you are getting files named mylog.2017-03-13 13:00
(including a space and hour and minute), that indicates that you have fussed with the .suffix
field in a way not shown in your example code. The precise-to-a-minute format (for when == 'm'
or when == 'M'
) is:
self.suffix = "%Y-%m-%d_%H-%M"
self.extMatch = r"^\d{4}-\d{2}-\d{2}_\d{2}-\d{2}$"
which has an underscore and a hyphen, not a space and a colon, at least in the source I am looking at.
You may also want to look at Using python logging from multiple modules with writing to a file and RotatingFileHandler for some general logging background.
1This "oldest" depends on string sort order, which requires that the names of the files sort alphabetically the same way they would numerically. Hence the suffix must be year first, then month, then day, then hour, and so on: using month names like Jan
, Feb
, Apr
would cause Apr
to come before—i.e., be older than—Feb
, which comes before Jan
, and so on.