python - Why does logging not work when running a Flask app with werkzeug? -


so here copy paste example reproduces problem.

import logging  flask import flask werkzeug.serving import run_simple 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 '<a href="/app/error">click error</a>'      @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 getting responses 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) 

to make error show navigate http://localhost:5000/app/error, want know why stack trace doesn't show in app.log file. assume dispatchermiddleware or run_simple somehow catching exception before can logged. if run app instance using app.run() error logging works fine.

the normal exception handler not called when app.debug = true. looking in code of app.py in flask:

def log_exception(self, exc_info):     """logs exception.  called :meth:`handle_exception`     if debugging disabled , right before handler called.     ^^^^^^^^^^^^^^^^^^^^^^^^     default implementation logs exception error on     :attr:`logger`. 

indeed, when setting app.debug = true exceptions propagation set true explicitly, prevents log_exception called. here excerpt of documentation (emphasis mine):

propagate_exceptions: explicitly enable or disable propagation of exceptions. if not set or explicitly set none implicitly true if either testing or debug true.

so, managed both werkzeug debugging , logging working happily little tweak , following code:

import logging  flask import flask werkzeug.serving import run_simple werkzeug.wsgi import dispatchermiddleware ## new code here import functools flask._compat import reraise  def my_log_exception(exc_info, original_log_exception=none):     original_log_exception(exc_info)     exc_type, exc, tb = exc_info     # re-raise werkzeug     reraise(exc_type, exc, tb) ##   def app_builder(app_name, log_file):     app = flask(app_name)     app.debug = true     app.config.update(propagate_exceptions=false)      handler = logging.filehandler(log_file)     handler.setlevel(logging.debug)     app.logger.addhandler(handler)      ## new code     app.log_exception = functools.partial(my_log_exception,  original_log_exception=app.log_exception)     ##      return app  # rest of code unchanged 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -