【发布时间】:2014-07-01 01:58:16
【问题描述】:
在我的一生中,当我通过 gunicorn 运行 Flask 应用程序时,我无法弄清楚我的错误来自哪里,因为我无法弄清楚如何显示堆栈跟踪。
例如,假设我有一个非常简单的“Hello,World!”用 Flask 编写的应用程序。
import logging
import os
from flask import Flask
app = Flask(__name__)
app.debug = True
@app.route('/')
def hello():
raise Exception('Exception raised!')
return 'Hello World!'
if __name__ == '__main__':
app.run()
如果我用python hello.py 运行它,那么一切都很好,因为我得到了一个非常有用的堆栈跟踪:
(venv)142:helloflask $ python hello.py
* Running on http://127.0.0.1:5000/
* Restarting with reloader
127.0.0.1 - - [30/Jun/2014 18:52:42] "GET / HTTP/1.1" 500 -
Traceback (most recent call last):
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Users/grautur/code/helloflask/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/grautur/code/helloflask/hello.py", line 10, in hello
raise Exception('Exception raised!')
Exception: Exception raised!
但是,如果我用
创建一个 Procfileweb: gunicorn hello:app
然后使用foreman start -p 8000 启动应用程序,然后我什么也看不到。只是一个“内部服务器错误”网页。
$ foreman start -p 8000
18:56:10 web.1 | started with pid 36850
(...nothing else is ever displayed...)
如何让我的 Flask+gunicorn 应用程序显示更多有用的调试消息?我尝试设置app.debug = True(及其各种迭代),因为它似乎与其他 StackOverflow 帖子的建议一样,但它似乎并没有改变任何东西。
【问题讨论】:
-
您好,您找到解决方案了吗?
-
Gunicorn 默认记录到 stderr,我认为这可以解释您所看到的唯一原因是 gunicorn 是否记录到文件(在这种情况下,您在 stderr 或 stdout 上什么也得不到)。顺便说一句,你试过吗? IE。配置 gunicorn 以使用
web: gunicorn --error-logfile=hello-gunicorn.log --access-logfile=hello-gunicorn-access.log hello:app登录到文件并查看那里登录的内容?