calebj-cogs icon indicating copy to clipboard operation
calebj-cogs copied to clipboard

[activitylog] Weekly log rotation generates incorrect rotation string

Open brandons209 opened this issue 6 years ago • 0 comments

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())

brandons209 avatar May 02 '19 05:05 brandons209