title: How to create a new log file every time the application runs?
body: <p>Currently, this is what I have (<code>testlog.py</code>):</p>
  <pre><code>import logging
  import logging.handlers
  
  filename = &quot;example.log&quot;
  
  logger = logging.getLogger()
  logger.setLevel(logging.DEBUG)
  formatter = logging.Formatter(&quot;%(asctime)s - %(name)s - %(levelname)s - %(message)s&quot;)
  
  handler = logging.handlers.RotatingFileHandler(filename, mode = 'w', backupCount = 5)
  handler.setLevel(logging.DEBUG)
  handler.setFormatter(formatter)
  logger.addHandler(handler)
  
  ch = logging.StreamHandler()
  ch.setLevel(logging.DEBUG)
  ch.setFormatter(formatter)
  logger.addHandler(ch)
  
  for i in range(10):
     logger.debug(&quot;testx&quot;) #where I alternate x from 1 thru 9 to see output
  </code></pre>
  <p>It currently successfully prints out to the console and to <code>example.log</code>, which is what I want.</p>
  <p>Every time I run it, it makes a new file and replaces the old <code>example.log</code> like so:</p>
  <ul>
  <li><p>run with <code>logger.debug(&quot;test1&quot;)</code> - <code>example.log</code> will contain <code>test1</code> 10 times like it should.</p>
  </li>
  <li><p>run with <code>logger.debug(&quot;test2&quot;)</code> - it rewrites <code>example.log</code> to contain <code>test2</code> 10 times.</p>
  </li>
  <li><p>etc...</p>
  </li>
  </ul>
  <p>However, I would like for the code to make <em><strong>a new log file</strong></em> every time I run the program so that I have:</p>
  <pre class="lang-none prettyprint-override"><code>example.log
  example.log1
  example.log2 
  ...
  example.log5
  </code></pre>
  <p>In conclusion, I'd like for this file to print the log message to the console, to the log file, and I would like a new log file (up to *.5) whenever I run the program.</p>