mirror of
https://github.com/python/cpython.git
synced 2025-07-29 06:05:00 +00:00
Fixed: #2914 (RFE for UTC support in TimedRotatingFileHandler) and #2929 (wrong filename used to delete old log files).
This commit is contained in:
parent
ae019e14bd
commit
a12da73c46
1 changed files with 20 additions and 12 deletions
|
@ -156,10 +156,11 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
|
||||||
If backupCount is > 0, when rollover is done, no more than backupCount
|
If backupCount is > 0, when rollover is done, no more than backupCount
|
||||||
files are kept - the oldest ones are deleted.
|
files are kept - the oldest ones are deleted.
|
||||||
"""
|
"""
|
||||||
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0):
|
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0, utc=0):
|
||||||
BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
|
BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
|
||||||
self.when = string.upper(when)
|
self.when = string.upper(when)
|
||||||
self.backupCount = backupCount
|
self.backupCount = backupCount
|
||||||
|
self.utc = utc
|
||||||
# Calculate the real rollover interval, which is just the number of
|
# Calculate the real rollover interval, which is just the number of
|
||||||
# seconds between rollovers. Also set the filename suffix used when
|
# seconds between rollovers. Also set the filename suffix used when
|
||||||
# a rollover occurs. Current 'when' events supported:
|
# a rollover occurs. Current 'when' events supported:
|
||||||
|
@ -214,7 +215,10 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
|
||||||
# the rest. Note that this code doesn't care about leap seconds. :)
|
# the rest. Note that this code doesn't care about leap seconds. :)
|
||||||
if self.when == 'MIDNIGHT' or self.when.startswith('W'):
|
if self.when == 'MIDNIGHT' or self.when.startswith('W'):
|
||||||
# This could be done with less code, but I wanted it to be clear
|
# This could be done with less code, but I wanted it to be clear
|
||||||
t = time.localtime(currentTime)
|
if utc:
|
||||||
|
t = time.gmtime(currentTime)
|
||||||
|
else:
|
||||||
|
t = time.localtime(currentTime)
|
||||||
currentHour = t[3]
|
currentHour = t[3]
|
||||||
currentMinute = t[4]
|
currentMinute = t[4]
|
||||||
currentSecond = t[5]
|
currentSecond = t[5]
|
||||||
|
@ -245,13 +249,14 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
|
||||||
else:
|
else:
|
||||||
daysToWait = 6 - day + self.dayOfWeek + 1
|
daysToWait = 6 - day + self.dayOfWeek + 1
|
||||||
newRolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
|
newRolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
|
||||||
dstNow = t[-1]
|
if not utc:
|
||||||
dstAtRollover = time.localtime(newRolloverAt)[-1]
|
dstNow = t[-1]
|
||||||
if dstNow != dstAtRollover:
|
dstAtRollover = time.localtime(newRolloverAt)[-1]
|
||||||
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
|
if dstNow != dstAtRollover:
|
||||||
newRolloverAt = newRolloverAt - 3600
|
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
|
||||||
else: # DST bows out before next rollover, so we need to add an hour
|
newRolloverAt = newRolloverAt - 3600
|
||||||
newRolloverAt = newRolloverAt + 3600
|
else: # DST bows out before next rollover, so we need to add an hour
|
||||||
|
newRolloverAt = newRolloverAt + 3600
|
||||||
self.rolloverAt = newRolloverAt
|
self.rolloverAt = newRolloverAt
|
||||||
|
|
||||||
#print "Will rollover at %d, %d seconds from now" % (self.rolloverAt, self.rolloverAt - currentTime)
|
#print "Will rollover at %d, %d seconds from now" % (self.rolloverAt, self.rolloverAt - currentTime)
|
||||||
|
@ -284,7 +289,7 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
|
||||||
if fileName[:plen] == prefix:
|
if fileName[:plen] == prefix:
|
||||||
suffix = fileName[plen:]
|
suffix = fileName[plen:]
|
||||||
if self.extMatch.match(suffix):
|
if self.extMatch.match(suffix):
|
||||||
result.append(fileName)
|
result.append(os.path.join(dirName, fileName))
|
||||||
result.sort()
|
result.sort()
|
||||||
if len(result) < self.backupCount:
|
if len(result) < self.backupCount:
|
||||||
result = []
|
result = []
|
||||||
|
@ -303,7 +308,10 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
|
||||||
self.stream.close()
|
self.stream.close()
|
||||||
# get the time that this sequence started at and make it a TimeTuple
|
# get the time that this sequence started at and make it a TimeTuple
|
||||||
t = self.rolloverAt - self.interval
|
t = self.rolloverAt - self.interval
|
||||||
timeTuple = time.localtime(t)
|
if self.utc:
|
||||||
|
timeTuple = time.gmtime(t)
|
||||||
|
else:
|
||||||
|
timeTuple = time.localtime(t)
|
||||||
dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
|
dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
|
||||||
if os.path.exists(dfn):
|
if os.path.exists(dfn):
|
||||||
os.remove(dfn)
|
os.remove(dfn)
|
||||||
|
@ -324,7 +332,7 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
|
||||||
while newRolloverAt <= currentTime:
|
while newRolloverAt <= currentTime:
|
||||||
newRolloverAt = newRolloverAt + self.interval
|
newRolloverAt = newRolloverAt + self.interval
|
||||||
#If DST changes and midnight or weekly rollover, adjust for this.
|
#If DST changes and midnight or weekly rollover, adjust for this.
|
||||||
if self.when == 'MIDNIGHT' or self.when.startswith('W'):
|
if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
|
||||||
dstNow = time.localtime(currentTime)[-1]
|
dstNow = time.localtime(currentTime)[-1]
|
||||||
dstAtRollover = time.localtime(newRolloverAt)[-1]
|
dstAtRollover = time.localtime(newRolloverAt)[-1]
|
||||||
if dstNow != dstAtRollover:
|
if dstNow != dstAtRollover:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue