Why is Python logging framework losing messages?
<p>I have a Python 3.4 application that uses logging extensively. I have two <code>FileHandler</code>s and a <code>StreamHandler</code> registered. Everything works as expected except that sometimes, and it seems to happen after the <code>requests</code> library throws an exception, the log files lose all the accumulated messages and start with new messages. I'm assuming that for some reason the <code>FileHandler</code>s reopened the files with <code>mode='w'</code>, but I don't understand why. Any ideas?</p>
<p>The main program sets up the loggers as follows:</p>
<pre><code># Set up root logger - two handlers logging to files
fh_debug = logging.FileHandler('Syncer.debug', mode='w', encoding='utf-8')
fh_debug.setLevel(logging.DEBUG)
fh_log = logging.FileHandler('Syncer.log', mode='w', encoding='utf-8')
fh_log.setLevel(logging.INFO)
fh_formatter = logging.Formatter(
    '%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
fh_debug.setFormatter(fh_formatter)
fh_log.setFormatter(fh_formatter)
logging.getLogger().addHandler(fh_debug)
logging.getLogger().addHandler(fh_log)

# Add console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
ch_formatter = logging.Formatter('%(message)s')
ch.setFormatter(ch_formatter)
logging.getLogger().addHandler(ch)

# Need to set the logging level of the logger as well as the handlers
logging.getLogger().setLevel(logging.DEBUG)

# Set up the logger for this module
logger = logging.getLogger(&quot;Syncer&quot;)

logger.debug('Logger started.')
</code></pre>
<p>The modules simply contain:</p>
<pre><code>logger = logging.getLogger(__name__)
</code></pre>