Why does logging not work when running a Flask app with werkzeug?
<p>So here is a copy paste example that reproduces the problem.</p>

<pre><code>import logging

from flask import Flask
from werkzeug.serving import run_simple
from werkzeug.wsgi import DispatcherMiddleware

def app_builder(app_name, log_file):

    app = Flask(app_name)
    app.debug = True

    handler = logging.FileHandler(log_file)
    handler.setLevel(logging.DEBUG)
    app.logger.addHandler(handler)

    return app

def _simple(env, resp):
    resp(b'200 OK', [(b'Content-Type', b'text/plain')])
    return [b'root']

if __name__ == "__main__":

    app = app_builder(app_name='app', log_file='app.log')

    @app.route('/')
    def index():
        return '&lt;a href="/app/error"&gt;click for error&lt;/a&gt;'

    @app.route('/error')
    def error():
        1/0
        return 'error page'

    app2 = app_builder(app_name='app2', log_file='app2.log')

    @app2.route('/')
    def index():
        return 'you are getting responses from app2'

    app.debug = True
    app2.debug = True

    application = DispatcherMiddleware(_simple, {
        '/app':     app,
        '/app2':    app2
        })

    run_simple(hostname='localhost',
               port=5000,
               application=application,
               use_reloader=True,
               use_debugger=True)
</code></pre>

<p>To make an error show up navigate to <code>http://localhost:5000/app/error</code>, I want to know why the stack trace doesn't show up in the <code>app.log</code> file. I assume that the <code>DispatcherMiddleware</code> or <code>run_simple</code> are somehow catching the exception before it can be logged. If I run only the <code>app</code> instance using <code>app.run()</code> the error logging works fine.</p>

<p>I found a <a href="https://gist.github.com/ibeex/3257877" rel="nofollow">gist</a> which talks about logging in flask. The comment by andyxning (commented on Apr 18, 2015) mentions this - <code>if app.debug is True then all log level above DEBUG will be logged to stderr(StreamHandler)</code>. </p>

<p>The comment also has a link to the source code of <a href="https://github.com/pallets/flask/blob/master/flask/logging.py" rel="nofollow">flask/logging.py</a>. The <code>create_logger</code> method creates an instance of <code>DebugHandler</code> which inherits from <code>StreamHandler</code> class.</p>

<p>If you print <code>app.logger.handlers</code> you can see that it has an object of <code>flask.logging.DebugHandler</code>. </p>

<pre><code>print app.logger.handlers
[&lt;flask.logging.DebugHandler object at 0x110315090&gt;]
</code></pre>

<p>This DebugHandler is probably used when <code>app.debug is set to true</code> and hence the stack trace gets printed on the console.</p>

<p>Hope this is what you are looking for.</p>

python flask wsgi werkzeug