Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc363936

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 commitfee86fd)Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent9eb7ed7 commitc363936

File tree

3 files changed

+373
-32
lines changed

3 files changed

+373
-32
lines changed

‎Lib/logging/handlers.py‎

Lines changed: 21 additions & 30 deletions
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-
ifr<0:
302+
ifr<=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-
ifnotself.utc:
333-
dstNow=t[-1]
334-
dstAtRollover=time.localtime(newRolloverAt)[-1]
335-
ifdstNow!=dstAtRollover:
336-
ifnotdstNow:# 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+
ifnotself.utc:
333+
dstNow=t[-1]
334+
dstAtRollover=time.localtime(result)[-1]
335+
ifdstNow!=dstAtRollover:
336+
ifnotdstNow:# DST kicks in before next rollover, so we need to deduct an hour
337+
addend=-3600
338+
ifnottime.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
returnresult
343344

344345
defshouldRollover(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-
ifself.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
ifself.utc:
426423
timeTuple=time.gmtime(t)
427424
else:
428425
timeTuple=time.localtime(t)
426+
dstNow=time.localtime(currentTime)[-1]
429427
dstThen=timeTuple[-1]
430428
ifdstNow!=dstThen:
431429
ifdstNow:
@@ -436,26 +434,19 @@ def doRollover(self):
436434
dfn=self.rotation_filename(self.baseFilename+"."+
437435
time.strftime(self.suffix,timeTuple))
438436
ifos.path.exists(dfn):
439-
os.remove(dfn)
437+
# Already rolled over.
438+
return
439+
440+
ifself.stream:
441+
self.stream.close()
442+
self.stream=None
440443
self.rotate(self.baseFilename,dfn)
441444
ifself.backupCount>0:
442445
forsinself.getFilesToDelete():
443446
os.remove(s)
444447
ifnotself.delay:
445448
self.stream=self._open()
446-
newRolloverAt=self.computeRollover(currentTime)
447-
whilenewRolloverAt<=currentTime:
448-
newRolloverAt=newRolloverAt+self.interval
449-
#If DST changes and midnight or weekly rollover, adjust for this.
450-
if (self.when=='MIDNIGHT'orself.when.startswith('W'))andnotself.utc:
451-
dstAtRollover=time.localtime(newRolloverAt)[-1]
452-
ifdstNow!=dstAtRollover:
453-
ifnotdstNow:# 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
classWatchedFileHandler(logging.FileHandler):
461452
"""

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp