logging how to control the times in which flush to log file
<p>I've to use logging module and wonder if there is a way to log into existing log file , by appending my data to the existing file and more important if I can control the times I flush into the file.</p>

<p>Currently I need to be able to flush all the time to the file because, there are cases in which the script running the logic with the logging can crash, so I need to know exactly where my program stopped.</p>

<p>Any ideas and code examples?
Thanks</p>

<p>If you use <code>logging.FileHandler</code> and choose an existing log file, by default it will append to that file.  The method that actually writes the log record is the <code>emit()</code> method on logging handlers.  If you look at the source code for the <code>FileHandler</code>, it <code>flush</code>'s after <em>every</em> write, so it should be doing what you want by default.</p>

<pre><code>import logging

log = logging.getLogger()
handler = logging.FileHandler('/path/to/log.txt')
log.addHandler(handler)

log.warning('This is a message')
</code></pre>

python python-2.7 logging flush