Flask application traceback doesn’t show up in server log

Run in development mode by setting the FLASK_ENV environment variable to development. Unhandled errors will show a stack trace in the terminal and the browser instead of a generic 500 error page.

export FLASK_ENV=development  # use `set` on Windows
flask run

Prior to Flask 1.0, use FLASK_DEBUG=1 instead.

If you’re still using app.run (no longer recommended in Flask 0.11), pass debug=True.

if __name__ == '__main__':
    app.run(debug=True)

In production, you don’t want to run your app in debug mode. Instead you should log the errors to a file.

Flask uses the standard Python logging library can be configured to log errors. Insert the the following to have send Flask’s log messages to a file.

import logging
handler = logging.FileHandler('/path/to/app.log')  # errors logged to this file
handler.setLevel(logging.ERROR)  # only log errors and above
app.logger.addHandler(handler)  # attach the handler to the app's logger

Read more about the Python logging module. In particular you may want to change where errors are logged, or change the level to record more than just errors.

Flask has documentation for configuring logging and handling errors.

Leave a Comment