This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: 'weekly' rotating logging file rotation incorrect
Type: behavior Stage:
Components: Library (Lib) Versions: Python 2.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: vinay.sajip Nosy List: christian.heimes, kmk, vinay.sajip
Priority: normal Keywords: easy

Created on 2008-01-15 21:30 by kmk, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (8)
msg59983 - (view) Author: Kathryn M Kowalski (kmk) Date: 2008-01-15 21:30
Log file did not 'rotate' on day requested.
Fixed code in Lib/logging/handlers.py class TimedRotatingFileHandler
Compare excerpt of my fix below to the original

# Case 2) The day to rollover is further in the interval (i.e., today is
#         day 2 (Wednesday) and rollover is on day 6 (Sunday).  Days to
#         next rollover is simply 6 - 2, or 4.
# Case 3) The day to rollover is behind us in the interval (i.e., today
#         is day 5 (Saturday) and rollover is on day 3 (Thursday).
#         Days to rollover is 6 - 5 + 3 + 1, or 5.  In this case, it's 
the
#         number of days left in the current week (1) plus the number
#         of days in the next week until the rollover day (4).
if when.startswith('W'):
   day = t[6] # 0 is Monday
   if self.dayOfWeek > day:
       daysToWait = (self.dayOfWeek - day)
       self.rolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
   if self.dayOfWeek < day:
       daysToWait = (6 - day) + self.dayOfWeek + 1
msg60000 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-01-16 17:09
Please can you post the data which caused the failure? Is 'the original'
the current trunk revision, or a Python release version? There was a
patch to this code not long ago, so I'd like to know which code you had
originally. Thanks.
msg60053 - (view) Author: Kathryn M Kowalski (kmk) Date: 2008-01-17 20:11
downloaded from ActiveState aug 2007 Python 2.5.1.1 

# Case 2) The day to rollover is further in the interval (i.e., today is
#         day 2 (Wednesday) and rollover is on day 6 (Sunday).  Days to
#         next rollover is simply 6 - 2 - 1, or 3.
# Case 3) The day to rollover is behind us in the interval (i.e., today
#         is day 5 (Saturday) and rollover is on day 3 (Thursday).
#         Days to rollover is 6 - 5 + 3, or 4.  In this case, it's the
#         number of days left in the current week (1) plus the number
#         of days in the next week until the rollover day (3).
if when.startswith('W'):
   day = t[6] # 0 is Monday
   if day > self.dayOfWeek:
       daysToWait = (day - self.dayOfWeek) - 1
       self.rolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
   if day < self.dayOfWeek:
       daysToWait = (6 - self.dayOfWeek) + day
msg60064 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-01-17 21:33
There's already been a change to this code, since 2.5.1.1. Here's the
code in trunk:

if when.startswith('W'):
    day = t[6] # 0 is Monday
    if day != self.dayOfWeek:
        if day < self.dayOfWeek:
            daysToWait = self.dayOfWeek - day - 1
        else:
            daysToWait = 6 - day + self.dayOfWeek
        self.rolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))

Does it work for you?
msg60113 - (view) Author: Kathryn M Kowalski (kmk) Date: 2008-01-18 20:30
I did not put suggested code in - walking through it and counting days 
on my fingers I don't think it works.

If the desired rollover day is Tuesday (self.dayOfWeek = 1) and today 
is Tuesday (day = 1) then self.rolloverAt is the seconds to midnight as 
if daysToWait =0, and it rolls over at midnight Tuesday.  However if 
the desired rollover day is Tuesday (self.dayOfWeek = 1) and today is 
Monday (day = 0) then daysToWait = 0 again.  It rolls over on Monday at 
midnight - not Tuesday at midnight.

If the desired rollover day is Tuesday (self.dayOfWeek = 1) and today 
is Wednesday (day = 2) then daysToWait = 5.  It also rolls over on 
Monday at midnight - not Tuesday at midnight.

Changing it to:
    day = t[6] # 0 is Monday
    if day != self.dayOfWeek:
        if day < self.dayOfWeek:
            daysToWait = self.dayOfWeek - day
        else:
            daysToWait = 6 - day + self.dayOfWeek + 1

would make it equivalent to what I have running and appears to work. 
(Always rolls (ends) on day specified at midnight.)

Alternatively, if you wanted to change it so the log starts on the day 
specified you could just get rid of "if day != self.dayOfWeek:" from 
your code.

if when.startswith('W'):
    day = t[6] # 0 is Monday
    if day < self.dayOfWeek:
        daysToWait = self.dayOfWeek - day - 1
    else:
        daysToWait = 6 - day + self.dayOfWeek
    self.rolloverAt = self.rolloverAt + (daysToWait * (60 * 60 * 24))
msg61417 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-01-21 17:06
Fix checked into trunk and release25-maint. Thanks for the patch, Kathryn.
msg61420 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2008-01-21 17:15
Please update Misc/NEWS for the bug fix. You forgot to update it for
this and another logging fix a week ago.
msg61433 - (view) Author: Vinay Sajip (vinay.sajip) * (Python committer) Date: 2008-01-21 18:19
Misc/NEWS updated. Thanks for the reminder!
History
Date User Action Args
2022-04-11 14:56:29adminsetgithub: 46158
2008-01-21 18:19:23vinay.sajipsetstatus: pending -> closed
2008-01-21 18:19:04vinay.sajipsetmessages: + msg61433
2008-01-21 17:15:24christian.heimessetstatus: closed -> pending
nosy: + christian.heimes
messages: + msg61420
2008-01-21 17:06:39vinay.sajipsetstatus: open -> closed
resolution: fixed
messages: + msg61417
2008-01-20 20:03:48christian.heimessetpriority: normal
keywords: + easy
2008-01-18 20:30:28kmksetmessages: + msg60113
2008-01-17 21:33:23vinay.sajipsetmessages: + msg60064
2008-01-17 20:12:00kmksetmessages: + msg60053
2008-01-16 17:09:07vinay.sajipsetassignee: vinay.sajip
messages: + msg60000
nosy: + vinay.sajip
2008-01-15 21:30:08kmkcreate