我有一个简单的 Flask 应用程序,但有一个错误:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
i = 1/0
return "<p>Hello, World!</p>"
我还有一个简单的uWSGI服务器(ini文件):
[uwsgi]
module = myapp:app
...
我希望将所有 Flask 错误重定向到一个单独的文件
error.log
。所以我的error.log
参观路线后应该是:
Traceback (most recent call last):
File "/opt/homebrew/lib/python3.10/site-packages/flask/app.py", line 2528, in wsgi_app
response = self.full_dispatch_request()
File "/opt/homebrew/lib/python3.10/site-packages/flask/app.py", line 1825, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/homebrew/lib/python3.10/site-packages/flask/app.py", line 1823, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/homebrew/lib/python3.10/site-packages/flask/app.py", line 1799, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
File "/Users/ctsahn/test.py", line 7, in hello_world
i = 1/0
ZeroDivisionError: division by zero
我尝试了几种方法来重定向 Flask 错误,包括添加
sys.stderr = open('error.log','wt')
我的 Flask 应用程序,以及尝试这个(和类似的方法):How to add stdout and stderr to logger file in flask
但是,错误不会被重定向,只会出现在 stderr 上。我怎样才能正确重定向?