python WatchedFileHandler still writing to old file after rotation
<p>I've been using <a href="https://docs.python.org/3/library/logging.handlers.html#watchedfilehandler" rel="nofollow">WatchedFileHandler</a> as my python logging file handler, so that I can rotate my logs with <code>logrotate</code> (on ubuntu 14.04), which you know is what the docs say its for. My <code>logrotate</code> config files looks like</p>

<pre><code>/path_to_logs/*.log {
        daily
        rotate 365
        size 10M
        compress
        delaycompress
        missingok
        notifempty
        su root root
}
</code></pre>

<p>Everything seemed to be working just fine. I'm using logstash to ship my logs to my elasticsearch cluster and everything is great. I added a second log file for my debug logs which gets rotated but is not watched by logstash. I noticed that when that file is rotated, python just keeps writing to <code>/path_to_debug_logs/*.log.1</code> and never starts writting to the new file. If I manually tail <code>/path_to_debug_logs/*.log.1</code>, it switches over instantly  and starts writing to <code>/path_to_debug_logs/*.log</code>.</p>

<p>This seems REALLY weird to me.</p>

<p>I believe what is happening is that logstash is always tailing my non-debug logs, which some how triggers the switch over to the new file after <code>logrotate</code> is called. If <code>logrotate</code> is called twice without a switch over, the log.1 file gets moved and compressed to log.2.gz, which python can no longer log to and logs are lost.</p>

<p>Clearly there are a bunch of hacky solutions to this (such as a cronjob that tails all my logs every now and then), but I feel like I must be doing something wrong.</p>

<p>I'm using <code>WatchedFileHandler</code> and <code>logrotate</code> instead of <code>RotatingFileHandler</code> for a number of reasons, but mainly because it will nicely compress my logs for me after rotation.</p>

<p>UPDATE:</p>

<p>I tried the horrible hack of adding a manual tail to the end of my log rotation config script.</p>

<pre><code>sharedscripts
postrotate
    /usr/bin/tail -n 1 path_to_logs/*.log.1
endscript
</code></pre>

<p>Sure enough this works most of the time, but randomly fails sometimes for no clear reason, so isn't a solution. I've also tried a number of less hacky solutions where I've modified the way <code>WatchFileHandler</code> checks if the file has changed, but no luck.</p>

<p>I'm fairly sure the root of my problem is that the logs are stored on a network drive, which is somehow confusing the file system.</p>

<p>I'm moving my rotation to python with <code>RotatingFileHandler</code>, but if anyone knows the proper way to handle this I'd love to know.</p>