Flask 应用程序回溯未显示在服务器日志中 [英] Flask application traceback doesn't show up in server log

查看:32
本文介绍了Flask 应用程序回溯未显示在服务器日志中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 uWSGI 和 nginx 运行我的 Flask 应用程序.有 500 错误,但回溯没有出现在浏览器或日志中.如何从 Flask 记录回溯?

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

uWSGI 日志只显示 500 状态码,不显示回溯.nginx 日志中也没有任何内容.

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)

推荐答案

通过将 FLASK_ENV 环境变量设置为 development 在开发模式下运行.未处理的错误将在终端和浏览器中显示堆栈跟踪,而不是一般的 500 错误页面.

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

在 Flask 1.0 之前,请改用 FLASK_DEBUG=1.

Prior to Flask 1.0, use FLASK_DEBUG=1 instead.

如果您仍在使用 app.run(在 Flask 0.11 中不再推荐),请传递 debug=True.

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 使用标准的 Python 日志库可以配置来记录错误.插入以下内容以将 Flask 的日志消息发送到文件.

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

阅读有关 Python logging 模块的更多信息.特别是您可能想要更改记录错误的位置,或者更改级别以记录更多错误.

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 有关于配置日志处理错误.

Flask has documentation for configuring logging and handling errors.

这篇关于Flask 应用程序回溯未显示在服务器日志中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆