Flask application traceback doesn't show up in server log
Asked Answered
L

5

23

I'm running my Flask application with uWSGI and nginx. There's a 500 error, but the traceback doesn't appear in the browser or the logs. How do I log the traceback from Flask?

uwsgi --http-socket 127.0.0.1:9000 --wsgi-file /var/webapps/magicws/service.py --module service:app --uid www-data --gid www-data --logto /var/log/magicws/magicapp.log

The uWSGI log only shows the 500 status code, not the traceback. There's also nothing in the nginx log.

[pid: 18343|app: 0|req: 1/1] 127.0.0.1 () {34 vars in 642 bytes} 
[Tue Sep 22 15:50:52 2015] 
GET /getinfo?color=White => generated 291 bytes in 64 msecs (HTTP/1.0 500) 
2 headers in 84 bytes (1 switches on core 0)
Luna answered 22/9, 2015 at 16:32 Comment(0)
E
31

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.

Edmee answered 22/9, 2015 at 16:53 Comment(2)
I don't want to look in a separate file, I just want to look in the main log output. How can I do that? My flask is running inside a docker container so having a separate file for errors is just cumbersome.Pridgen
@LennartRolland to log to the standard error, you just need to use handler = logging.StreamHandler(sys.stderr)Uruguay
L
1

You can set the FLASK_DEBUG=1 environment variable when running the app as a service. Only do this temporarily, and note that enabling debug mode on a production server is a security issue.

Upstart (default in Ubuntu 14.04)

# /etc/init/uwsgiapp.conf
env FLASK_DEBUG=1
script
  // upstart exec section
end script

Systemd (default in Ubuntu 16.04, Arch)

[Service]
Environment="FLASK_DEBUG=1"
# other parts

Supervisord

[program:flask]
environment=FLASK_DEBUG=1

Typically the logs will be somewhere in /var/log/.

Levin answered 8/1, 2017 at 16:24 Comment(0)
M
0

You need to check the user and group permission in your code. You can see it using "top" command.

Miguelinamiguelita answered 6/3, 2019 at 11:2 Comment(0)
P
0

You can use the Flask-Debug extension as an alternative. Of course, this should never be enabled on production.

from flask import Flask
from flask_debug import Debug
app = Flask(__name__)
Debug(app)
app.run(debug=True)

Next, go to http://localhost:5000/_debug to preview the logs.

flask-appconfig>=0.10 supports automatic initialization of Flask-Debug while developing, allowing you to completely omit it from your own code (and therefore production deployments).

Preindicate answered 14/4, 2019 at 21:19 Comment(0)
K
0

You can set the exc_info=True when you log the error:

from flask import current_app, jsonify    

@users.route("/")
def get_users():
    try:
        users = get_users()  
        return jsonify(users), 200
    except Exception as e:
        current_app.logger.error(e, exc_info=True)
        return jsonify({"message": str(e)}), 400
Kaylakayle answered 21/1, 2024 at 22:48 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.