By Phil Cote


2015-09-22 16:32:34 8 Comments

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)

2 comments

@lv9 2015-09-22 16:53:14

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.

@I Am Batman 2017-01-08 16:24:29

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/.

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] How to disable request logging in Django and uWSGI?

  • 2016-03-23 18:45:46
  • serg
  • 5211 View
  • 10 Score
  • 2 Answer
  • Tags:   django uwsgi

0 Answered Questions

Replicate uwsgi default logging

  • 2018-07-11 18:56:11
  • jonny
  • 17 View
  • 0 Score
  • 0 Answer
  • Tags:   logging uwsgi

1 Answered Questions

[SOLVED] Upstream timed out (uWSGI + NGINX + Flask)

1 Answered Questions

uwsgi working with nginx, systemd emperor not working with same configs and setup

3 Answered Questions

2 Answered Questions

[SOLVED] Nginx - Rewrite the request_uri before uwsgi_pass

0 Answered Questions

Can't run Flask application under Apache

1 Answered Questions

[SOLVED] Nginx/Uwsgi log showing duplicate requests

  • 2015-04-24 14:49:13
  • ThrowsException
  • 342 View
  • 1 Score
  • 1 Answer
  • Tags:   python flask uwsgi

2 Answered Questions

[SOLVED] Nginx + uWSGI + Flask app performance test with ab

1 Answered Questions

Request is too slow with nginx, uwsgi, django, pgpool, postgresql

  • 2012-07-18 16:10:49
  • shchoi
  • 2049 View
  • 0 Score
  • 1 Answer
  • Tags:   django nginx uwsgi

Sponsored Content