Flask应用程序追踪不会显示在服务器日志中 [英] Flask application traceback doesn't show up in server log
问题描述
$ b
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状态码,而不是回溯。
$ b
[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 =>在64个微秒(HTTP / 1.0 500)中产生291个字节
在84个字节中的2个标题(核心0上的1个开关)
在调试模式下通过在 app.run
被调用之前添加此行来运行
$ b
app.debug = True
<
$ b
app.run(debug = True)
/ pre>
现在堆栈跟踪应该出现在终端和浏览器中,而不是通用的500错误页面。
<当使用新的flask
命令运行服务器时,设置环境变量FLASK_DEBUG
。FLASK_DEBUG = 1 flask run
在生产中,您不想在调试模式下运行您的应用程序。相反,您应该将错误记录到一个文件中。
Flask使用标准的Python日志记录库可以配置为记录错误。插入以下内容以将Flask的日志消息发送到文件中。
pre $ import $ logging $ b $ handler = logging.FileHandler( '/path/to/app.log')#记录到这个文件的错误
handler.setLevel(logging.ERROR)#只记录错误和
app.logger.addHandler(handler)#附加处理程序到应用程序的记录器
阅读更多关于Python logging 模块。特别是你可能想把文件处理程序切换到 RotatingFileHandler
来确保日志文件不会变得太大。您可能还想更改记录级别以记录更多的错误。
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
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)
Run in debug mode by adding this line before app.run
gets called
app.debug = True
or by running with
app.run(debug=True)
Now a stack trace should appear in the terminal and the browser instead of a generic 500 error page.
When using the new flask
command to run the server, set the environment variable FLASK_DEBUG
.
FLASK_DEBUG=1 flask run
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 switch the file handler to a RotatingFileHandler
to ensure log files don't grow too large. You may also want to change the logging level to record more than just errors.
Flask also has instructions on how to log your application.
这篇关于Flask应用程序追踪不会显示在服务器日志中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!