Skip to content

TimedRotatingFileHandler "midnight" misleading when interval > 1 #90535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mschiess mannequin opened this issue Jan 14, 2022 · 6 comments
Closed

TimedRotatingFileHandler "midnight" misleading when interval > 1 #90535

mschiess mannequin opened this issue Jan 14, 2022 · 6 comments
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@mschiess
Copy link
Mannequin

mschiess mannequin commented Jan 14, 2022

BPO 46377
Nosy @vsajip
PRs
  • bpo-46377: TimedRotatingFileHandler fix to ignore interval when "midnight" is given #30599
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2022-01-14.13:50:48.156>
    labels = ['type-feature', 'library', '3.11']
    title = 'TimedRotatingFileHandler "midnight" misleading when interval > 1'
    updated_at = <Date 2022-01-14.18:25:21.751>
    user = 'https://bugs.python.org/mschiess'

    bugs.python.org fields:

    activity = <Date 2022-01-14.18:25:21.751>
    actor = 'mschiess'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2022-01-14.13:50:48.156>
    creator = 'mschiess'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 46377
    keywords = ['patch']
    message_count = 5.0
    messages = ['410558', '410565', '410567', '410571', '410583']
    nosy_count = 3.0
    nosy_names = ['vinay.sajip', 'python-dev', 'mschiess']
    pr_nums = ['30599']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue46377'
    versions = ['Python 3.11']

    Linked PRs

    @mschiess
    Copy link
    Mannequin Author

    mschiess mannequin commented Jan 14, 2022

    Using the TimedRotatingFileHandler along with "when='midnight'" and interval > 1, midnight is handled equally to "days" which is a little misleading.

    Expectation:
    setting when to 'midnight', the file is rotated every midnight (interval value should be ignored)

    Current behavior:
    If 'midnight' is given alongside with an interval greater than 1 (.e.g 5), the (internal) interval (24*60*60) is calculated with the given interval -> 24*60*60 * 5.

    In my case, this led to some unforeseeable and unexpected behavior.

    @mschiess mschiess mannequin added type-bug An unexpected behavior, bug, or error 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes 3.11 only security fixes stdlib Python modules in the Lib dir labels Jan 14, 2022
    @vsajip
    Copy link
    Member

    vsajip commented Jan 14, 2022

    Making a change without considering backward compatibility is premature. I've closed the PR as there appears to be something wrong with it - it references hundreds of changed files. Did you look at the 'atTime' keyword parameter of TimedRotatingFileHandler?

    @mschiess
    Copy link
    Mannequin Author

    mschiess mannequin commented Jan 14, 2022

    i've just checked PR and you're right, something with the PR went wrong.

    Anyway, midnight (at least from the wording) specifies the "atTime". (which should be midnight).

    Again, if there's (by mistake) an interval bigger than 1 set(which in my mind makes no sense along to be used with midnight) things are getting pretty intransparent. The midnight handler created a logfile dated with 2021-12-15 (last night). Took me some time to get this sorted. (I've discovered, that I've set 30 in a default value file).

    Agreed on the backward compatibility, but I would assume someone using "midnight" would not expect any other behavior than "daily at midnight" besides using the atTime to modify the rollover time. (my opinion)

    @vsajip
    Copy link
    Member

    vsajip commented Jan 14, 2022

    Unfortunately, you can't rely on people always doing "the sensible thing", for any number of good reasons. If a particular set of parameter values didn't cause failure, it is probably used somewhere.

    Anyway, your problem goes away if interval == 1, right? If we were to tighten things up (e.g. disallowing interval > 1 with "midnight"), then it would have to be done on a deprecation cycle at the very least, ISTM.

    @iritkatriel iritkatriel added type-feature A feature request or enhancement and removed 3.7 (EOL) end of life 3.8 (EOL) end of life 3.9 only security fixes 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Jan 14, 2022
    @mschiess
    Copy link
    Mannequin Author

    mschiess mannequin commented Jan 14, 2022

    Yes, enforcing interval == 1 or interval == None (which pulls the TimedRotatingFileHandler class __init__ default value which is also 1) works perfectly with midnight.

    I do not see any urge on that topic - as I personally now know the issue :D - but I really feel this fix could save someones else's time someday ;)

    So going the "safe" way via deprecation cycle seems to be the right approach

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    vsajip added a commit to vsajip/cpython that referenced this issue Oct 8, 2022
    @vsajip vsajip added 3.12 only security fixes and removed 3.11 only security fixes labels Oct 8, 2022
    @erlend-aasland erlend-aasland added 3.13 bugs and security fixes and removed 3.12 only security fixes labels Jan 5, 2024
    @serhiy-storchaka
    Copy link
    Member

    I see now that the problem is that computeRollover() returns the next nearest "midnight" time, ignoring the interval parameter, while doRollover() computes the file name by stepping back interval days. And there is the same issue with the weekly rotation.

    My fix #116191 perhaps affected this issue and made it worse (if you do not consider it better). Previously, in doRollover(), if computeRollover() returned the current time, self.interval was added, so the next rollover time was set after interval days or weeks. But this behavior depended on atTime and the time of running the script.

    Perhaps the safest way is to implement the correct support of the interval parameter for "midnight" and weekly rotation.

    serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 1, 2024
    …eHandler
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 1, 2024
    …eHandler
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    serhiy-storchaka added a commit that referenced this issue Mar 16, 2024
    …er (GH-116220)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 16, 2024
    …eHandler (pythonGH-116220)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    (cherry picked from commit 269051d)
    
    Co-authored-by: Serhiy Storchaka <[email protected]>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 16, 2024
    …eHandler (pythonGH-116220)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    (cherry picked from commit 269051d)
    
    Co-authored-by: Serhiy Storchaka <[email protected]>
    serhiy-storchaka added a commit that referenced this issue Mar 18, 2024
    …leHandler (GH-116220) (GH-116892)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    (cherry picked from commit 269051d)
    
    Co-authored-by: Serhiy Storchaka <[email protected]>
    serhiy-storchaka added a commit that referenced this issue Mar 18, 2024
    …leHandler (GH-116220) (GH-116893)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    (cherry picked from commit 269051d)
    
    Co-authored-by: Serhiy Storchaka <[email protected]>
    vstinner pushed a commit to vstinner/cpython that referenced this issue Mar 20, 2024
    …eHandler (pythonGH-116220)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
    …eHandler (pythonGH-116220)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
    …eHandler (pythonGH-116220)
    
    Fix support of interval values > 1 in logging.TimedRotatingFileHandler
    for when='MIDNIGHT' and when='Wx'.
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    Development

    No branches or pull requests

    4 participants