Flask 记录器在与 Uvicorn 等 ASGI 服务器一起使用时不起作用
Flask logger does not work when served with an ASGI server like Uvicorn
我想使用名为 Uvicorn 的 ASGI 服务器为我的 Flask 应用程序提供服务。结果是当我将我的应用程序从 wsgi 转换为 asgi 时(按照此处的说明 https://flask.palletsprojects.com/en/2.0.x/deploying/asgi/)突然 app.logger 函数未定义。但是我真的需要日志记录功能来开发我的应用程序。
请按照以下步骤重现问题。
我写了这个基本的应用程序
from flask import Flask
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
@app.route("/")
def hello_world():
app.logger.info("hey I am your favourite log message")
return "<p>Hello, World!</p>"
app = WsgiToAsgi(app)
我安装了 uvicorn
pip install uvicorn
运行 我的应用
uvicorn app:app
当应用程序尝试使用 app.logger
登录时,我收到此错误
ERROR: Exception in ASGI application
Traceback (most recent call last):
File "f:\projects\logger-flask\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 373, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "f:\projects\logger-flask\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 21, in __call__
await WsgiToAsgiInstance(self.wsgi_application)(scope, receive, send)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 51, in __call__
await self.run_wsgi_app(body)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\sync.py", line 444, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 455, in wait_for
return await fut
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\sync.py", line 486, in thread_handler
return func(*args, **kwargs)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 140, in run_wsgi_app
for output in self.wsgi_application(environ, self.start_response):
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File ".\app.py", line 8, in hello_world
app.logger.info("hey I am your favourite log message")
AttributeError: 'WsgiToAsgi' object has no attribute 'logger'
Flask
的一个实例支持WSGI协议,而且还增加了一些便利,比如logger。如果你包装了 Flask 实例并失去了对它的引用,你就失去了那些方便的方法。
而不是
app = WsgiToAsgi(app)
尝试
wsgi = WsgiToAsgi(app)
然后
uvicorn app:wsgi
我为 WsgiToAsgi 实例使用了不同的名称,它删除了初始异常。但是我仍然收到日志记录错误。
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1084, in emit
stream.write(msg + self.terminator)
TypeError: a bytes-like object is required, not 'str'
为了解决这个问题,我添加了这两行来删除默认的日志记录处理程序
from flask.logging import default_handler
app.logger.removeHandler(default_handler)
我的完整工作代码在这里
from flask import Flask, current_app
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
from flask.logging import default_handler
app.logger.removeHandler(default_handler)
@app.route("/")
def hello_world():
app.logger.warning('hey I am your favourite log message')
return "<p>Hello, World!</p>"
wsgi = WsgiToAsgi(app)
我想使用名为 Uvicorn 的 ASGI 服务器为我的 Flask 应用程序提供服务。结果是当我将我的应用程序从 wsgi 转换为 asgi 时(按照此处的说明 https://flask.palletsprojects.com/en/2.0.x/deploying/asgi/)突然 app.logger 函数未定义。但是我真的需要日志记录功能来开发我的应用程序。
请按照以下步骤重现问题。
我写了这个基本的应用程序
from flask import Flask
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
@app.route("/")
def hello_world():
app.logger.info("hey I am your favourite log message")
return "<p>Hello, World!</p>"
app = WsgiToAsgi(app)
我安装了 uvicorn
pip install uvicorn
运行 我的应用
uvicorn app:app
当应用程序尝试使用 app.logger
登录时,我收到此错误ERROR: Exception in ASGI application
Traceback (most recent call last):
File "f:\projects\logger-flask\venv\lib\site-packages\uvicorn\protocols\http\h11_impl.py", line 373, in run_asgi
result = await app(self.scope, self.receive, self.send)
File "f:\projects\logger-flask\venv\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 75, in __call__
return await self.app(scope, receive, send)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 21, in __call__
await WsgiToAsgiInstance(self.wsgi_application)(scope, receive, send)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 51, in __call__
await self.run_wsgi_app(body)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\sync.py", line 444, in __call__
ret = await asyncio.wait_for(future, timeout=None)
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\asyncio\tasks.py", line 455, in wait_for
return await fut
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\thread.py", line 57, in run
result = self.fn(*self.args, **self.kwargs)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\sync.py", line 486, in thread_handler
return func(*args, **kwargs)
File "f:\projects\logger-flask\venv\lib\site-packages\asgiref\wsgi.py", line 140, in run_wsgi_app
for output in self.wsgi_application(environ, self.start_response):
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2088, in __call__
return self.wsgi_app(environ, start_response)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2073, in wsgi_app
response = self.handle_exception(e)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 2070, in wsgi_app
response = self.full_dispatch_request()
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1515, in full_dispatch_request
rv = self.handle_user_exception(e)
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1513, in full_dispatch_request
rv = self.dispatch_request()
File "f:\projects\logger-flask\venv\lib\site-packages\flask\app.py", line 1499, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
File ".\app.py", line 8, in hello_world
app.logger.info("hey I am your favourite log message")
AttributeError: 'WsgiToAsgi' object has no attribute 'logger'
Flask
的一个实例支持WSGI协议,而且还增加了一些便利,比如logger。如果你包装了 Flask 实例并失去了对它的引用,你就失去了那些方便的方法。
而不是
app = WsgiToAsgi(app)
尝试
wsgi = WsgiToAsgi(app)
然后
uvicorn app:wsgi
我为 WsgiToAsgi 实例使用了不同的名称,它删除了初始异常。但是我仍然收到日志记录错误。
--- Logging error ---
Traceback (most recent call last):
File "C:\Users\Tamjid\AppData\Local\Programs\Python\Python38\lib\logging\__init__.py", line 1084, in emit
stream.write(msg + self.terminator)
TypeError: a bytes-like object is required, not 'str'
为了解决这个问题,我添加了这两行来删除默认的日志记录处理程序
from flask.logging import default_handler
app.logger.removeHandler(default_handler)
我的完整工作代码在这里
from flask import Flask, current_app
from asgiref.wsgi import WsgiToAsgi
app = Flask(__name__)
from flask.logging import default_handler
app.logger.removeHandler(default_handler)
@app.route("/")
def hello_world():
app.logger.warning('hey I am your favourite log message')
return "<p>Hello, World!</p>"
wsgi = WsgiToAsgi(app)