1
- # Copyright 2001-2012 by Vinay Sajip. All Rights Reserved.
1
+ # Copyright 2001-2013 by Vinay Sajip. All Rights Reserved.
2
2
#
3
3
# Permission to use, copy, modify, and distribute this software and its
4
4
# documentation for any purpose and without fee is hereby granted,
18
18
Additional handlers for the logging package for Python. The core package is
19
19
based on PEP 282 and comments thereto in comp.lang.python.
20
20
21
- Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
21
+ Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
22
22
23
23
To use, simply 'import logging.handlers' and log away!
24
24
"""
@@ -196,11 +196,12 @@ class TimedRotatingFileHandler(BaseRotatingHandler):
196
196
If backupCount is > 0, when rollover is done, no more than backupCount
197
197
files are kept - the oldest ones are deleted.
198
198
"""
199
- def __init__ (self , filename , when = 'h' , interval = 1 , backupCount = 0 , encoding = None , delay = False , utc = False ):
199
+ def __init__ (self , filename , when = 'h' , interval = 1 , backupCount = 0 , encoding = None , delay = False , utc = False , atTime = None ):
200
200
BaseRotatingHandler .__init__ (self , filename , 'a' , encoding , delay )
201
201
self .when = when .upper ()
202
202
self .backupCount = backupCount
203
203
self .utc = utc
204
+ self .atTime = atTime
204
205
# Calculate the real rollover interval, which is just the number of
205
206
# seconds between rollovers. Also set the filename suffix used when
206
207
# a rollover occurs. Current 'when' events supported:
@@ -270,9 +271,22 @@ def computeRollover(self, currentTime):
270
271
currentHour = t [3 ]
271
272
currentMinute = t [4 ]
272
273
currentSecond = t [5 ]
273
- # r is the number of seconds left between now and midnight
274
- r = _MIDNIGHT - ((currentHour * 60 + currentMinute ) * 60 +
275
- currentSecond )
274
+ currentDay = t [6 ]
275
+ # r is the number of seconds left between now and the next rotation
276
+ if self .atTime is None :
277
+ rotate_ts = _MIDNIGHT
278
+ else :
279
+ rotate_ts = ((self .atTime .hour * 60 + self .atTime .minute )* 60 +
280
+ self .atTime .second )
281
+
282
+ r = rotate_ts - ((currentHour * 60 + currentMinute ) * 60 +
283
+ currentSecond )
284
+ if r < 0 :
285
+ # Rotate time is before the current time (for example when
286
+ # self.rotateAt is 13:45 and it now 14:15), rotation is
287
+ # tomorrow.
288
+ r += _MIDNIGHT
289
+ currentDay = (currentDay + 1 ) % 7
276
290
result = currentTime + r
277
291
# If we are rolling over on a certain day, add in the number of days until
278
292
# the next rollover, but offset by 1 since we just calculated the time
@@ -290,7 +304,7 @@ def computeRollover(self, currentTime):
290
304
# This is because the above time calculation takes us to midnight on this
291
305
# day, i.e. the start of the next day.
292
306
if self .when .startswith ('W' ):
293
- day = t [ 6 ] # 0 is Monday
307
+ day = currentDay # 0 is Monday
294
308
if day != self .dayOfWeek :
295
309
if day < self .dayOfWeek :
296
310
daysToWait = self .dayOfWeek - day
0 commit comments