如何在Flask + gunicorn应用程序中查看异常?

时间:2014-07-01 01:58:17

标签: python flask gunicorn

对于我的生活,当我通过gunicorn运行它时,我无法弄清楚我的Flask应用程序中的错误来自哪里,因为我无法弄清楚如何显示堆栈跟踪

例如,我们说我有一个非常简单的" Hello,World!"用烧瓶写的应用程序。

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!

但是,如果我使用

创建一个Procfile
web: 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帖子一样,但它似乎没有改变任何东西。

2 个答案:

答案 0 :(得分:6)

我不熟悉Foreman,所以不确定是否需要以任何方式进行配置,但是对于Gunicorn记录任何内容,首先需要为Flask应用设置日志记录。

这样做的简单方法如下:

import logging

# Log only in production mode.
if not app.debug:
    stream_handler = logging.StreamHandler()
    stream_handler.setLevel(logging.INFO)
    app.logger.addHandler(stream_handler)
默认情况下,

StreamHandler会记录到 stderr 。如果您想要登录文件,Gunicorn有--access-logfile--error-logfile选项。

有关Flask错误处理的更多详细说明和一些好主意,请参阅docs

答案 1 :(得分:0)

我发现当使用Upstart脚本(Ubuntu 14.04 LTS)激活Gunicorn时,Flask的错误被写入/var/log/upstart/YOUR_UPSTART_SCRIPT.log

无论我尝试了什么,例如@AudriusKažukauskas的解决方案,我在任何地方都看不到任何错误或追溯。