calebj-cogs
calebj-cogs copied to clipboard
[activitylog] Weekly log rotation generates incorrect rotation string
When rotating logs weekly, if the current day is in the next month and within the weekly log that started in a day in the previous month, you can get a negative value when doing timestamp.days - timestamp.weekday(). This will result in no logs being saved until the next rotation period.
Simply using a timedelta to perform the calculation, instead of manually, solves the issue: start = timestamp - timedelta(days=timestamp.weekday()) Which makes weekly log rotations happen every Monday.
def format_rotation_string(timestamp, rotation_code, filename=None):
kwargs = dict(hour=0, minute=0, second=0, microsecond=0)
if not rotation_code:
return filename or ''
if rotation_code == 'y':
kwargs.update(day=1, month=1)
start = timestamp.replace(**kwargs)
elif rotation_code == 'm':
kwargs.update(day=1)
start = timestamp.replace(**kwargs)
elif rotation_code == 'w':
start = timestamp - timedelta(days=timestamp.weekday())