pytest logbook logging to file and stdout
<p>I'm trying to setup <code>logbook</code> in a PyTest test to output everything to both <code>stderr</code> and a file.  The file should get every log level, but <code>stderr</code> should have a higher threshold (which PyTest will manage with it's usual capture settings).</p>

<p>I've got the <code>pytest-logbook</code> plugin. That redirects <code>stderr</code> into PyTest capture, but I'm not sure how to add the file output.</p>

<p>This is (hopefully) obvious to someone that knows logbook, but it's new to me.</p>

<p>One more thing, I want the file logging to be real time. My tests are generally long running and PyTest's normal behavior of only showing output on failure isn't helping when I need to see if things are hung.</p>

<p>Here is code that I think should work, but doesn't. I get the log file, but nothing to <code>stdout</code>/<code>stderr</code> (even on fail):</p>

<p><code>conftest.py</code>:</p>

<pre><code>import os
import pytest
import logbook
import sys

@pytest.fixture(scope='module')
def modlog(request):
    """Logger that also writes to a file."""
    name = request.module.__name__
    if name.startswith('test_'):
        name = name[5:]
    logname = 'TEST-'+name+'.log'
    if os.path.exists(logname):
        os.rename(logname, logname+"~")
    logger = logbook.Logger(name)
    logger.handlers.append(logbook.FileHandler(logname, level='DEBUG'))
    logger.handlers.append(logbook.StreamHandler(sys.stdout, level='INFO'))
    logger.warn("Start of logging")
    return logger
</code></pre>

<p><code>test_loggy.py</code>:</p>

<pre><code>import pytest

def test_foo(modlog):
    modlog.info('hello')
    modlog.info('world')
    assert 0                       # logs will only print on test fail
</code></pre>