cpython: eead4be1bdd9 (original) (raw)
Mercurial > cpython
changeset 83271:eead4be1bdd9
Closed #9556: Allowed specifying a time-of-day for a TimedRotatingFileHandler to rotate. [#9556]
Vinay Sajip <vinay_sajip@yahoo.co.uk> | |
---|---|
date | Fri, 12 Apr 2013 17:04:23 +0100 |
parents | 95eed7a76c72 |
children | 5118304a4c9c |
files | Doc/library/logging.handlers.rst Lib/logging/handlers.py Lib/test/test_logging.py Misc/NEWS |
diffstat | 4 files changed, 73 insertions(+), 8 deletions(-)[+] [-] Doc/library/logging.handlers.rst 8 Lib/logging/handlers.py 28 Lib/test/test_logging.py 42 Misc/NEWS 3 |
line wrap: on
line diff
--- a/Doc/library/logging.handlers.rst
+++ b/Doc/library/logging.handlers.rst
@@ -296,7 +296,7 @@ The :class:TimedRotatingFileHandler
cl
timed intervals.
-.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False)
+.. class:: TimedRotatingFileHandler(filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None)
Returns a new instance of the :class:TimedRotatingFileHandler
class. The
specified file is opened and used as the stream for logging. On rotating it also
@@ -346,6 +346,12 @@ timed intervals.
If delay is true, then file opening is deferred until the first call to
:meth:emit
.
- If atTime is not
None
, it must be adatetime.time
instance which - specifies the time of day when rollover occurs, for the cases where rollover
- is set to happen "at midnight" or "on a particular weekday". +
- .. versionchanged:: 3.4
*atTime* parameter was added.[](#l1.21)
--- a/Lib/logging/handlers.py +++ b/Lib/logging/handlers.py @@ -1,4 +1,4 @@ -# Copyright 2001-2012 by Vinay Sajip. All Rights Reserved. +# Copyright 2001-2013 by Vinay Sajip. All Rights Reserved. #
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
@@ -18,7 +18,7 @@
Additional handlers for the logging package for Python. The core package is
based on PEP 282 and comments thereto in comp.lang.python.
-Copyright (C) 2001-2012 Vinay Sajip. All Rights Reserved.
+Copyright (C) 2001-2013 Vinay Sajip. All Rights Reserved.
To use, simply 'import logging.handlers' and log away
"""
@@ -196,11 +196,12 @@ class TimedRotatingFileHandler(BaseRotat
If backupCount is > 0, when rollover is done, no more than backupCount
files are kept - the oldest ones are deleted.
"""
- def init(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False):
- def init(self, filename, when='h', interval=1, backupCount=0, encoding=None, delay=False, utc=False, atTime=None): BaseRotatingHandler.init(self, filename, 'a', encoding, delay) self.when = when.upper() self.backupCount = backupCount self.utc = utc
self.atTime = atTime[](#l2.28) # Calculate the real rollover interval, which is just the number of[](#l2.29) # seconds between rollovers. Also set the filename suffix used when[](#l2.30) # a rollover occurs. Current 'when' events supported:[](#l2.31)
@@ -270,9 +271,22 @@ class TimedRotatingFileHandler(BaseRotat currentHour = t[3] currentMinute = t[4] currentSecond = t[5]
# r is the number of seconds left between now and midnight[](#l2.36)
r = _MIDNIGHT - ((currentHour * 60 + currentMinute) * 60 +[](#l2.37)
currentSecond)[](#l2.38)
currentDay = t[6][](#l2.39)
# r is the number of seconds left between now and the next rotation[](#l2.40)
if self.atTime is None:[](#l2.41)
rotate_ts = _MIDNIGHT[](#l2.42)
else:[](#l2.43)
rotate_ts = ((self.atTime.hour * 60 + self.atTime.minute)*60 +[](#l2.44)
self.atTime.second)[](#l2.45)
r = rotate_ts - ((currentHour * 60 + currentMinute) * 60 +[](#l2.47)
currentSecond)[](#l2.48)
if r < 0:[](#l2.49)
# Rotate time is before the current time (for example when[](#l2.50)
# self.rotateAt is 13:45 and it now 14:15), rotation is[](#l2.51)
# tomorrow.[](#l2.52)
r += _MIDNIGHT[](#l2.53)
currentDay = (currentDay + 1) % 7[](#l2.54) result = currentTime + r[](#l2.55) # If we are rolling over on a certain day, add in the number of days until[](#l2.56) # the next rollover, but offset by 1 since we just calculated the time[](#l2.57)
@@ -290,7 +304,7 @@ class TimedRotatingFileHandler(BaseRotat # This is because the above time calculation takes us to midnight on this # day, i.e. the start of the next day. if self.when.startswith('W'):
day = t[6] # 0 is Monday[](#l2.62)
day = currentDay # 0 is Monday[](#l2.63) if day != self.dayOfWeek:[](#l2.64) if day < self.dayOfWeek:[](#l2.65) daysToWait = self.dayOfWeek - day[](#l2.66)
--- a/Lib/test/test_logging.py +++ b/Lib/test/test_logging.py @@ -3949,6 +3949,48 @@ class TimedRotatingFileHandlerTest(BaseF assertRaises(ValueError, logging.handlers.TimedRotatingFileHandler, self.fn, 'W7', delay=True)
- def test_compute_rollover_daily_attime(self):
currentTime = 0[](#l3.8)
atTime = datetime.time(12, 0, 0)[](#l3.9)
rh = logging.handlers.TimedRotatingFileHandler([](#l3.10)
self.fn, when='MIDNIGHT', interval=1, backupCount=0, utc=True,[](#l3.11)
atTime=atTime)[](#l3.12)
actual = rh.computeRollover(currentTime)[](#l3.14)
self.assertEqual(actual, currentTime + 12 * 60 * 60)[](#l3.15)
actual = rh.computeRollover(currentTime + 13 * 60 * 60)[](#l3.17)
self.assertEqual(actual, currentTime + 36 * 60 * 60)[](#l3.18)
rh.close()[](#l3.20)
- def test_compute_rollover_weekly_attime(self):
currentTime = 0[](#l3.23)
atTime = datetime.time(12, 0, 0)[](#l3.24)
wday = datetime.datetime.fromtimestamp(currentTime).weekday()[](#l3.26)
for day in range(7):[](#l3.27)
rh = logging.handlers.TimedRotatingFileHandler([](#l3.28)
self.fn, when='W%d' % day, interval=1, backupCount=0, utc=True,[](#l3.29)
atTime=atTime)[](#l3.30)
if wday > day:[](#l3.32)
expected = (7 - wday + day)[](#l3.33)
else:[](#l3.34)
expected = (day - wday)[](#l3.35)
expected *= 24 * 60 * 60[](#l3.36)
expected += 12 * 60 * 60[](#l3.37)
actual = rh.computeRollover(currentTime)[](#l3.38)
self.assertEqual(actual, expected)[](#l3.39)
if day == wday:[](#l3.40)
# goes into following week[](#l3.41)
expected += 7 * 24 * 60 * 60[](#l3.42)
actual = rh.computeRollover(currentTime + 13 * 60 * 60)[](#l3.43)
self.assertEqual(actual, expected)[](#l3.44)
rh.close()[](#l3.46)
+ + def secs(**kw): return datetime.timedelta(**kw) // datetime.timedelta(seconds=1)
--- a/Misc/NEWS +++ b/Misc/NEWS @@ -34,6 +34,9 @@ Core and Builtins Library ------- +- Issue #9556: Allowed specifying a time-of-day for a TimedRotatingFileHandler