Why Python's RotatingFileHandler of logging module will rename log file instead of create a new one?
<p>I have read the <code>handlers.py</code>of Python's logging module, and found that the logic of rotating log is unreasonable. The follow picture is its comments:
<a href="http://i.stack.imgur.com/0n3Ul.png" rel="nofollow"><img src="http://i.stack.imgur.com/0n3Ul.png" alt="enter image description here"></a></p>

<p>I think this logic is unreasonable especially in Windows: If some other processes hold the handler of the log file, then the renaming will fail. For example, I have two processes, one process find it is time to rotate file, so it close its own handler and rename the log. But this time, the log file's handler is held by another process, so an IO Exception will be raised because the log file can not be renamed now. That is why we say logging module is unsafe in multi-process.</p>

<p>My question is: Why not create another log file directly? Why does it need to rename the log file?</p>

<p>Suppose the log named <code>console.log</code>, I think the logic should be like this:</p>

<pre><code>if there is not console.log in destination folder:
    create console.log and logging info
else:
    add the suffix to the log file name.(e.g.: console.log.1)
    create the file and logging info

if the log file gets filled up:
    close the handler of the log file
    increase the suffix number
    create console.log.`newsuffix`(e.g.:console.log.2) and continue to logging info
</code></pre>

<p>In this logic, there is no need to rename the file, so there will not raise the IO Exception.</p>

<p>Can you find some bugs if the rotatingFileHandler of Python's logging module use my logic?</p>

<p>The main problem with your proposal is that the actual logging file always will be changing (first will be console.log, later will be console.log.1, after that console.log.2 etc...) so, all the programs that will read the logs, they will have to implement logic to find what is the newer file, the main problem is that the software reading logs will need to constantly scan the logs folder to find if a new file is created and start to read logs from it.</p>

python logging