New submission from Ed Catmur <e...@catmur.uk>:

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)
and if some code logs during shutdown, after logging has closed its handlers 
(asyncio is prone to do this, e.g.),
then logging.FileHandler._open will reopen the log file with the same mode as 
it was originally opened with, potentially truncating it and losing valuable 
information:

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 issue 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

----------
components: Library (Lib)
messages: 381146
nosy: ecatmur2
priority: normal
severity: normal
status: open
title: logging reopens file with same mode, possibly truncating
type: behavior
versions: Python 3.8

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue42378>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to