-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
logging reopens file with same mode, possibly truncating #86544
Comments
If a logging.FileHandler is configured with mode='w', or if logging.basicConfig is called with filemode='w' (as suggested by the Basic Logging Tutorial https://docs.python.org/3/howto/logging.html#logging-basic-tutorial) import atexit
atexit.register(lambda: logging.info("so long"))
import logging
logging.basicConfig(filename='test.log', filemode='w', level=logging.INFO)
logging.info("super important stuff") $ python truncate.py
$ cat test.log
INFO:root:so long I have a hunch that the fix to bpo-26789 will potentially make things worse, as previously at least there was a chance that logging had been fully unloaded so that the call to open would fail. https://stackoverflow.com/questions/39838616/why-is-python-logging-framework-losing-messages |
I've put up a PR here: https://github.com/python/cpython/pull/27310/files If this looks good, I will update the docs and add news entry. |
I believe I'm seeing this, still, in an async situation. It seems like the obvious culprit. When will this go out in a release? I'm on 3.10.1 from December (under Arch). The PR got merged to master in July but I went through all changelogs back to March and don't see it listed. |
<- I'm intentionally using mode 'w' (to support development) and it's never been an issue until I recently refactored the project to be asynchronous. Now, every time I fail, I suddenly lose the logs. Not awesome. |
I guess it got missed during the 3.10 beta cycle by not being backported - it might have needed to be cherry-picked. I'll see about getting it backported to 3.10. |
Can we close this, or is there more work left? |
I think we can close it. |
Python logging to a file with mode='w' can cause log loss when logging happens after the logger has already been cleaned up. See issue: python/cpython#86544 The fix for Python was to have special cases for files opened in mode='w'. Currently we use mode='w+', so the checked in fix does not solve our problem. I've implemented moving the file_logger to mode='a' and an explicit deletion of the logging file before opening the FileHandler.
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: