python - Using Flask's "after_request" handler to get tracebacks of errors -
the documentation after_request
says "as of flask 0.7 function might not executed @ end of request in case unhandled exception occurred." there way change after_request
functions called unhandled exceptions, example log traceback?
use teardown_request
instead.
register function run @ end of each request, regardless of whether there exception or not.
these functions not allowed modify request, , return values ignored. if exception occurred while processing request, gets passed each teardown_request function.
from flask import flask app = flask(__name__) # unhandled teardown won't happen while in debug mode # app.debug = true # set if need full traceback, not exception # app.config['propagate_exceptions'] = true @app.route('/') def index(): print(test) @app.teardown_request def log_unhandled(e): if e not none: print(repr(e)) # app.logger.exception(e) # works propagate_exceptions app.run('localhost')
note time teardown_request
called, traceback has fallen out of scope; exception available. can change setting propagate_exceptions = true
, although may have performance issues. given traceback logged flask already, may easier configure logging rather trying log yourself.
Comments
Post a Comment