【问题标题】:How to see exceptions in a Flask + gunicorn app?如何在 Flask + gunicorn 应用程序中查看异常?
【发布时间】: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!

但是,如果我用

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

【问题讨论】:

  • 您好,您找到解决方案了吗?
  • Gunicorn 默认记录到 stderr,我认为这可以解释您所看到的唯一原因是 gunicorn 是否记录到文件(在这种情况下,您在 stderr 或 stdout 上什么也得不到)。顺便说一句,你试过吗? IE。配置 gunicorn 以使用 web: gunicorn --error-logfile=hello-gunicorn.log --access-logfile=hello-gunicorn-access.log hello:app 登录到文件并查看那里登录的内容?

标签: python flask gunicorn


【解决方案1】:

我对 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

【讨论】:

  • Python 还有 logging.FileHandlerRotatingFileHandlerTimedRotatingFileHandler 等等
【解决方案2】:

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

无论我尝试了什么,例如@Audrius Kažukauskas 的解决方案,我根本看不到任何错误或回溯。

【讨论】:

    猜你喜欢
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    相关资源
    最近更新 更多