Logs are lost due to log rotation of logging module
<p>I have written some devops related migration tool in python which runs for several hours (like 50-60 hours for each cluster migration activity). I used python's logging module in the tool to log relevant information. The log automatically rotates every 24 hours. As a result, the old log file gets zipped into .gz format and a new empty file (with same name) gets created. However, in the new file I cannot find any logs (that I thought would contain logs after the log rotation)</p>

<p>I tried googling this problem, but couldn't find relevant information. Would highly appreciate any help regarding this.</p>

<p>code snippet:
<code>
import logging
LOG = logging.getLogger(<strong>name</strong>)</p>

<p>def setup_logging(logfile, levelName):
    filename = logfile
    try:
        level = getattr(logging, levelName.upper())
    except:
        print 'Invalid loglevel:%s' % levelName
    logging.basicConfig(filename=filename,
                        level=level,
                        format='%(asctime)s [%(levelname)s] [%(filename)s:%(lineno)s - %(funcName)s()] %(message)s')</p>

<p></code></p>

<p>After setting up logging like that I use something like:</p>

<p>LOG.info('') / LOG.warning('')</p>

<p>Ok. So I found a way to handle this kind of scenario by using WatchedFileHandler as suggested here:</p>

<p><a href="http://stackoverflow.com/questions/9106795/python-logging-and-rotating-files">Python logging and rotating files</a></p>

python python-2.7 logging