title: How can I combine stdlib logging with py.test
body: <p>I am using py.test to test some modules of mine that contains quite a bit of stdlib logging. I would of course like for the logging to log to stdout, which is captured by py.test, so that I will get all relevant logging messages if a test fails. </p>
  
  <p>The problem with this is that the logging module ends up trying to log messages to the 'stdout'-object provided by py.test <em>after</em> this object has been discarded by py.test. That is, I get:</p>
  
  <pre><code>Traceback (most recent call last):
    File "/usr/lib/python2.6/atexit.py", line 24, in _run_exitfuncs
      func(*targs, **kargs)
    File "/usr/lib/python2.6/logging/__init__.py", line 1508, in shutdown
      h.flush()
    File "/usr/lib/python2.6/logging/__init__.py", line 754, in flush
      self.stream.flush()
  ValueError: I/O operation on closed file
  </code></pre>
  
  <p>If I turn off capturing with <code>-s</code>, I don't have any problems, but of course that makes the test output unreadable with irrelevant logging.</p>
  
  <p>Can anyone tell me the proper way to integrate stdlib logging with py.test?</p>
  
  <p>(I tried looking at <a href="https://stackoverflow.com/questions/4673373/logging-within-py-test-tests">this</a>, where it looks like it should just work without issues, so it didn't help me much)</p>