How to use Python's RotatingFileHandler
<p>I'm trying to do a test run of the <code>logging</code> module's <code>RotatingFileHandler</code> as follows:</p>

<pre><code>import logging
from logging.handlers import RotatingFileHandler

# logging.basicConfig(filename="example.log", level=logging.DEBUG)

logger = logging.getLogger('my_logger')
handler = RotatingFileHandler("my_log.log", maxBytes=2000, backupCount=10)
logger.addHandler(handler)

for _ in range(10000):
    logger.debug("Hello, world!")
</code></pre>

<p>However, with <code>logging.basicConfig</code> line commented out, the resulting <code>my_log.log</code> file contains no data:</p>

<p><a href="https://i.stack.imgur.com/Q3CoP.png" rel="nofollow"><img src="https://i.stack.imgur.com/Q3CoP.png" alt="enter image description here"></a></p>

<p>If I comment in the line with <code>logging.basicConfig(filename="example.log", level=logging.DEBUG)</code>, I get the expected <code>my_log.log</code> files with numbered suffixes. However, there is also the <code>example.log</code> which is a (relatively) large file:</p>

<p><a href="https://i.stack.imgur.com/J77C7.png" rel="nofollow"><img src="https://i.stack.imgur.com/J77C7.png" alt="enter image description here"></a></p>

<p>How can I set up the logging so that it only generates the <code>my_log.log</code> files, and not the large <code>example.log</code> file?</p>

<p>Python provides 5 logging levels out of the box (in increasing order of severity): <code>DEBUG</code>, <code>INFO</code>, <code>WARNING</code>, <code>ERROR</code> and <code>CRITICAL</code>. The default one <a href="https://docs.python.org/3/howto/logging.html#what-happens-if-no-configuration-is-provided" rel="nofollow">is <code>WARNING</code></a>. The docs says, that</p>

<blockquote>
  <p>Logging messages which are less severe than <em>lvl</em> will be ignored.</p>
</blockquote>

<p>So if you use <code>.debug</code> with the default settings, you won't see anything in your logs.</p>

<p>The easiest fix would be to use <code>logger.warning</code> function rather that <code>logger.debug</code>:</p>

<pre><code>import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger('my_logger')
handler = RotatingFileHandler('my_log.log', maxBytes=2000, backupCount=10)
logger.addHandler(handler)

for _ in range(10000):
    logger.warning('Hello, world!')
</code></pre>

<p>And if you want to change logger level you can use <a href="https://docs.python.org/3/library/logging.html#logging.Logger.setLevel" rel="nofollow"><code>.setLevel</code></a> method:</p>

<pre><code>import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler('my_log.log', maxBytes=2000, backupCount=10)
logger.addHandler(handler)

for _ in range(10000):
    logger.debug('Hello, world!')
</code></pre>

python logging