Skip to content

Commit 4a583e2

Browse files
author
vinay.sajip
committed
Fixed: #2914 (RFE for UTC support in TimedRotatingFileHandler) and #2929 (wrong filename used to delete old log files).
git-svn-id: http://svn.python.org/projects/python/trunk@63507 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent c12f312 commit 4a583e2

1 file changed

Lines changed: 20 additions & 12 deletions

File tree

Lib/logging/handlers.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,11 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
156156
If backupCount is > 0, when rollover is done, no more than backupCount
157157
files are kept - the oldest ones are deleted.
158158
"""
159-
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0):
159+
def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=0, utc=0):
160160
BaseRotatingHandler.__init__(self, filename, 'a', encoding, delay)
161161
self.when = string.upper(when)
162162
self.backupCount = backupCount
163+
self.utc = utc
163164
# Calculate the real rollover interval, which is just the number of
164165
# seconds between rollovers. Also set the filename suffix used when
165166
# a rollover occurs. Current 'when' events supported:
@@ -214,7 +215,10 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
214215
# the rest. Note that this code doesn't care about leap seconds. :)
215216
if self.when == 'MIDNIGHT' or self.when.startswith('W'):
216217
# This could be done with less code, but I wanted it to be clear
217-
t = time.localtime(currentTime)
218+
if utc:
219+
t = time.gmtime(currentTime)
220+
else:
221+
t = time.localtime(currentTime)
218222
currentHour = t[3]
219223
currentMinute = t[4]
220224
currentSecond = t[5]
@@ -245,13 +249,14 @@ def __init__(self, filename, when='h', interval=1, backupCount=0, encoding=None,
245249
else:
246250
daysToWait = 6 - day + self.dayOfWeek + 1
247251
newRolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
248-
dstNow = t[-1]
249-
dstAtRollover = time.localtime(newRolloverAt)[-1]
250-
if dstNow != dstAtRollover:
251-
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
252-
newRolloverAt = newRolloverAt - 3600
253-
else: # DST bows out before next rollover, so we need to add an hour
254-
newRolloverAt = newRolloverAt + 3600
252+
if not utc:
253+
dstNow = t[-1]
254+
dstAtRollover = time.localtime(newRolloverAt)[-1]
255+
if dstNow != dstAtRollover:
256+
if not dstNow: # DST kicks in before next rollover, so we need to deduct an hour
257+
newRolloverAt = newRolloverAt - 3600
258+
else: # DST bows out before next rollover, so we need to add an hour
259+
newRolloverAt = newRolloverAt + 3600
255260
self.rolloverAt = newRolloverAt
256261

257262
#print "Will rollover at %d, %d seconds from now" % (self.rolloverAt, self.rolloverAt - currentTime)
@@ -284,7 +289,7 @@ def getFilesToDelete(self):
284289
if fileName[:plen] == prefix:
285290
suffix = fileName[plen:]
286291
if self.extMatch.match(suffix):
287-
result.append(fileName)
292+
result.append(os.path.join(dirName, fileName))
288293
result.sort()
289294
if len(result) < self.backupCount:
290295
result = []
@@ -303,7 +308,10 @@ def doRollover(self):
303308
self.stream.close()
304309
# get the time that this sequence started at and make it a TimeTuple
305310
t = self.rolloverAt - self.interval
306-
timeTuple = time.localtime(t)
311+
if self.utc:
312+
timeTuple = time.gmtime(t)
313+
else:
314+
timeTuple = time.localtime(t)
307315
dfn = self.baseFilename + "." + time.strftime(self.suffix, timeTuple)
308316
if os.path.exists(dfn):
309317
os.remove(dfn)
@@ -324,7 +332,7 @@ def doRollover(self):
324332
while newRolloverAt <= currentTime:
325333
newRolloverAt = newRolloverAt + self.interval
326334
#If DST changes and midnight or weekly rollover, adjust for this.
327-
if self.when == 'MIDNIGHT' or self.when.startswith('W'):
335+
if (self.when == 'MIDNIGHT' or self.when.startswith('W')) and not self.utc:
328336
dstNow = time.localtime(currentTime)[-1]
329337
dstAtRollover = time.localtime(newRolloverAt)[-1]
330338
if dstNow != dstAtRollover:

0 commit comments

Comments
 (0)