Skip to content

Commit c363936

Browse files
[3.11] gh-88352: Fix logging.TimedRotatingFileHandler (GH-116191) (GH-116209)
* Do not overwrite already rolled over files. It happened at midnight or during the DST change and caused the loss of data. * computeRollover() now always return the timestamp larger than the specified time. * Fix computation of the rollover time during the DST change. (cherry picked from commit fee86fd) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9eb7ed7 commit c363936

File tree

3 files changed

+373
-32
lines changed

3 files changed

+373
-32
lines changed

Lib/logging/handlers.py

+21-30
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ def computeRollover(self, currentTime):
299299

300300
r = rotate_ts - ((currentHour * 60 + currentMinute) * 60 +
301301
currentSecond)
302-
if r < 0:
302+
if r <= 0:
303303
# Rotate time is before the current time (for example when
304304
# self.rotateAt is 13:45 and it now 14:15), rotation is
305305
# tomorrow.
@@ -328,17 +328,18 @@ def computeRollover(self, currentTime):
328328
daysToWait = self.dayOfWeek - day
329329
else:
330330
daysToWait = 6 - day + self.dayOfWeek + 1
331-
newRolloverAt = result + (daysToWait * (60 * 60 * 24))
332-
if not self.utc:
333-
dstNow = t[-1]
334-
dstAtRollover = time.localtime(newRolloverAt)[-1]
335-
if dstNow != dstAtRollover:
336-
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
337-
addend = -3600
338-
else: # DST bows out before next rollover, so we need to add an hour
339-
addend = 3600
340-
newRolloverAt += addend
341-
result = newRolloverAt
331+
result += daysToWait * (60 * 60 * 24)
332+
if not self.utc:
333+
dstNow = t[-1]
334+
dstAtRollover = time.localtime(result)[-1]
335+
if dstNow != dstAtRollover:
336+
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
337+
addend = -3600
338+
if not time.localtime(result-3600)[-1]:
339+
addend = 0
340+
else: # DST bows out before next rollover, so we need to add an hour
341+
addend = 3600
342+
result += addend
342343
return result
343344

344345
def shouldRollover(self, record):
@@ -415,17 +416,14 @@ def doRollover(self):
415416
then we have to get a list of matching filenames, sort them and remove
416417
the one with the oldest suffix.
417418
"""
418-
if self.stream:
419-
self.stream.close()
420-
self.stream = None
421419
# get the time that this sequence started at and make it a TimeTuple
422420
currentTime = int(time.time())
423-
dstNow = time.localtime(currentTime)[-1]
424421
t = self.rolloverAt - self.interval
425422
if self.utc:
426423
timeTuple = time.gmtime(t)
427424
else:
428425
timeTuple = time.localtime(t)
426+
dstNow = time.localtime(currentTime)[-1]
429427
dstThen = timeTuple[-1]
430428
if dstNow != dstThen:
431429
if dstNow:
@@ -436,26 +434,19 @@ def doRollover(self):
436434
dfn = self.rotation_filename(self.baseFilename + "." +
437435
time.strftime(self.suffix, timeTuple))
438436
if os.path.exists(dfn):
439-
os.remove(dfn)
437+
# Already rolled over.
438+
return
439+
440+
if self.stream:
441+
self.stream.close()
442+
self.stream = None
440443
self.rotate(self.baseFilename, dfn)
441444
if self.backupCount > 0:
442445
for s in self.getFilesToDelete():
443446
os.remove(s)
444447
if not self.delay:
445448
self.stream = self._open()
446-
newRolloverAt = self.computeRollover(currentTime)
447-
while newRolloverAt <= currentTime:
448-
newRolloverAt = newRolloverAt + self.interval
449-
#If DST changes and midnight or weekly rollover, adjust for this.
450-
if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
451-
dstAtRollover = time.localtime(newRolloverAt)[-1]
452-
if dstNow != dstAtRollover:
453-
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
454-
addend = -3600
455-
else: # DST bows out before next rollover, so we need to add an hour
456-
addend = 3600
457-
newRolloverAt += addend
458-
self.rolloverAt = newRolloverAt
449+
self.rolloverAt = self.computeRollover(currentTime)
459450

460451
class WatchedFileHandler(logging.FileHandler):
461452
"""

0 commit comments

Comments
 (0)