title: Python logging creates empty files
body: <p>I am using python's logging facility. I only want a log file created if there is something to log. In other words, no log file should be created if there was nothing to log.</p>
  
  <p>As soon as</p>
  
  <pre><code>logging.basicConfig( filename=filename_log, level=logging.DEBUG, mode='w')
  </code></pre>
  
  <p>is ran, an empty log file is created. I try to delete it before exiting with:</p>
  
  <pre><code>if ( os.stat( filename_log ).st_size == 0 ): os.remove( filename_log)
  </code></pre>
  
  <p>which gives an error message:</p>
  
  <pre><code>WindowsError: [Error 32] The process cannot access the file because it is being used by another process
  </code></pre>
  
  <p>So I suppose something else should be done before.</p>
  
  <p>So, is there any way not to have empty log files generated without writing my own logging procedure?</p>
  
  <p>In short: </p>
  
  <pre><code>logging.basicConfig( filename=filename_log, level=logging.DEBUG, mode='w')
  logging.debug('This message should go to the log file')
  </code></pre>
  
  <p>does log properly but gives an empty file if there is nothing to be logged. And</p>
  
  <pre><code>with open( filename_log, 'w') as logfile:
      logging.basicConfig( stream=logfile, level=logging.DEBUG)
  </code></pre>
  
  <p>gives a: <code>ValueError: I/O operation on closed file</code></p>