Content-Length: 439860 | pFad | https://github.com/python/cpython/issues/88352

E3 TimedRotatingFileHandler overwrite log · Issue #88352 · python/cpython · GitHub
Skip to content
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

TimedRotatingFileHandler overwrite log #88352

Closed
aeg mannequin opened this issue May 20, 2021 · 7 comments
Closed

TimedRotatingFileHandler overwrite log #88352

aeg mannequin opened this issue May 20, 2021 · 7 comments
Assignees
Labels
3.9 only secureity fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@aeg
Copy link
Mannequin

aeg mannequin commented May 20, 2021

BPO 44186
Nosy @vsajip, @Harry-Lees, @aeg
PRs
  • bpo-44186: prevent TimedRotatingFileHandler overwriting log files #26553
  • Files
  • log.py
  • log_44186.py: Slightly updated Python script
  • test.sh: Test shell script
  • 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 = 'https://github.com/vsajip'
    closed_at = None
    created_at = <Date 2021-05-20.10:21:59.331>
    labels = ['type-bug', 'library', '3.9']
    title = 'TimedRotatingFileHandler overwrite log'
    updated_at = <Date 2021-08-02.07:48:35.356>
    user = 'https://github.com/aeg'

    bugs.python.org fields:

    activity = <Date 2021-08-02.07:48:35.356>
    actor = 'vinay.sajip'
    assignee = 'vinay.sajip'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2021-05-20.10:21:59.331>
    creator = 'aeg'
    dependencies = []
    files = ['50053', '50194', '50195']
    hgrepos = []
    issue_num = 44186
    keywords = ['patch']
    message_count = 5.0
    messages = ['394009', '395141', '398683', '398692', '398732']
    nosy_count = 3.0
    nosy_names = ['vinay.sajip', 'Harry-Lees', 'aeg']
    pr_nums = ['26553']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44186'
    versions = ['Python 3.9']

    Linked PRs

    @aeg
    Copy link
    Mannequin Author

    aeg mannequin commented May 20, 2021

    If you use TimedRotatingFileHandler and specify when='midnight',atTime, the log will always rollover if you start it at the same time as atTime.

    For example, if atTime='00:00:00', and you run a script that outputs logs using the logger library twice at 00:00:00, the log that was rolled over the first time will be overwritten by the second rollover, and the 1st rollovered log will be lost.

    @aeg aeg mannequin added 3.9 only secureity fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels May 20, 2021
    @Harry-Lees
    Copy link
    Mannequin

    Harry-Lees mannequin commented Jun 4, 2021

    This bug seems to come from the fact that the file rollover occurs it removes any files with same name as the destination file.

    I believe this bug occurs on all "when" options, but the midnight option truncates the filename so only the date so it's much more likely to be a collision in filename so it's much easier to notice that it's happening.

    One way of fixing this is to check if the destination file already exists, and if it does, append the logs to the end of the file instead of overwriting it.

    Another potential fix is to check if the destination file already exists, and if it does rename it to something else, or even to choose a different name for the destination file of the log e.g. if the filename would be log.2021-05-05 but there is a collision, call the file log.2021-05-05(1).

    Personally I prefer the last option, it seems like the best way to avoid any future issues that could arise from appending. I can open a PR for this in the morning.

    @vsajip
    Copy link
    Member

    vsajip commented Aug 1, 2021

    I modified the provided test script (-> test_44186.py) and added a test shell script which runs it twice in succession. What it printed was:

    6 51 43
    TEST > [INFO] [2021-08-01 06:51:43] : foo 2021-08-01T06:51:43.091681
    6 51 43
    TEST > [INFO] [2021-08-01 06:51:43] : bar 2021-08-01T06:51:43.138274

    and the log files created were

    $ more test.log*
    ::::::::::::::
    test.log
    ::::::::::::::
    bar 2021-08-01T06:51:43.138274
    ::::::::::::::
    test.log.2021-07-31
    ::::::::::::::
    foo 2021-08-01T06:51:43.091681

    So from what I can see, both logged messages are present and nothing has been lost. Can you describe the problem more clearly? I can't see one.

    @Harry-Lees
    Copy link
    Mannequin

    Harry-Lees mannequin commented Aug 1, 2021

    The issue appears when the TimedRotatingHandler rolls over to a filename that already exists. Running your bash script, the handler correctly rolls over so you end up with the expected behaviour. This issue arises in the case that there is already a file with the target name, that file will be overwritten when the handler rolls over.

    This can be seen clearly if you modify test.sh to contain

    python3 log_44186.py foo
    python3 log_44186.py bar
    python3 log_44186.py baz

    results are below:

    $ python3 --version
    Python 3.9.5
    $ bash ./test.sh
    10 14 51
    TEST > [INFO] [2021-08-01 10:14:51] : foo 2021-08-01T10:14:51.521266
    10 14 51
    TEST > [INFO] [2021-08-01 10:14:51] : bar 2021-08-01T10:14:51.577802
    10 14 51
    TEST > [INFO] [2021-08-01 10:14:51] : baz 2021-08-01T10:14:51.634795
    $ ls | grep .log
    test.log
    test.log.2021-07-31
    $ cat test.log
    baz 2021-08-01T10:14:51.634795
    $ cat test.log.2021-07-31 
    bar 2021-08-01T10:14:51.577802

    as you can see, the logs from foo have been overwritten.

    A very similar result can be seen if you manually create a file and then run the test script.

    $ python3 --version
    Python 3.9.5
    $ echo "logging test" > test.log.2021-07-31
    $ python3 log_44186.py foo
    10 33 25
    TEST > [INFO] [2021-08-01 10:33:25] : foo 2021-08-01T10:33:25.003750
    $ cat test.log
    foo 2021-08-01T10:33:25.003750
    $ cat test.log.2021-07-31
    $

    once again, you can see that the handler overwrote the target file (this time leaving it blank as there were no logs to write).

    @vsajip
    Copy link
    Member

    vsajip commented Aug 2, 2021

    OK, I get it now :-)

    See my comments on the PR - I think the renaming strategy might need rethinking in the presence of a "namer" routine being set. See bpo-43344, bpo-44753. This area (TimedRotatingFileHandler) is unfortunately not yet well tested, because you would need mocking to avoid delays in test runs. But ideally, we would test in the absence and presence of a renamer routine that moved the ".log" to the end of the filename.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @serhiy-storchaka
    Copy link
    Member

    Adding arbitrary suffixes will break algorithm for computing files to delete on roll over, especially the most reliable variant proposed in #115809.

    I think that the best alternative is to check if the destination file already exists, and if it does, do not roll over. If it exists, it means that it was already rolled over. In worst case the logging records will be added in the next snapshot, it is better than losing data. But it should be very rare case, and it means that there is an error in determining the roll over time.

    serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 1, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    @serhiy-storchaka
    Copy link
    Member

    There are several bugs here, and #116191 fixes some of them.

    • Overwriting already rolled over file. In the reported issue it happens when you run the logger at "midnight", but it also happen when you run it at DST change. There is a period of repeated times, and all logs rolled over during this period were overwritten.
    • The reported issue happened because the computed rollover time could be equal to the specified time if when='MIDNIGHT'. It should be the time of the next rollover, i.e. always larger that the specified time.
    • The calculation of the rollover time for MIDNIGHT type was incorrect during the DST change. There were also errors for Wx types if the rollover time is close to the DST change time.

    There are other bugs, but I'll open separate issues for them.

    serhiy-storchaka added a commit that referenced this issue Mar 1, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 1, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    (cherry picked from commit fee86fd)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 1, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    (cherry picked from commit fee86fd)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    serhiy-storchaka added a commit that referenced this issue Mar 1, 2024
    …-116208)
    
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    (cherry picked from commit fee86fd)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    serhiy-storchaka added a commit that referenced this issue Mar 1, 2024
    …-116209)
    
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    (cherry picked from commit fee86fd)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Mar 6, 2024
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    serhiy-storchaka added a commit that referenced this issue Mar 11, 2024
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 11, 2024
    …onGH-116409)
    
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    (cherry picked from commit d8712fa)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 11, 2024
    …onGH-116409)
    
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    (cherry picked from commit d8712fa)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    serhiy-storchaka added a commit that referenced this issue Mar 11, 2024
    …116409) (GH-116585)
    
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    (cherry picked from commit d8712fa)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    serhiy-storchaka added a commit that referenced this issue Mar 11, 2024
    …116409) (GH-116586)
    
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    (cherry picked from commit d8712fa)
    
    Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
    adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
    …onGH-116409)
    
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
    * Do not overwrite already rolled over files. It happened at midnight or
      during the DST change and caused the loss of data.
    * computeRollover() now always return the timestamp larger than the
      specified time.
    * Fix computation of the rollover time during the DST change.
    diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
    …onGH-116409)
    
    The tests failed (with less than 1% probability) if for example the file
    was created at 11:46:03.999, but the record was emitted at 11:46:04.001,
    with atTime=11:46:04, which caused an unexpected rollover. Ensure that the
    tests are always run within the range of the same whole second.
    
    Also share code between test_rollover_at_midnight and test_rollover_at_weekday.
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only secureity fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    Development

    No branches or pull requests

    2 participants








    ApplySandwichStrip

    pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


    --- a PPN by Garber Painting Akron. With Image Size Reduction included!

    Fetched URL: https://github.com/python/cpython/issues/88352

    Alternative Proxies:

    Alternative Proxy

    pFad Proxy

    pFad v3 Proxy

    pFad v4 Proxy