Search code examples
pythonflasklogginguvicornasgi

Flask logger does not work when served with an ASGI server like Uvicorn


I want to serve my Flask app with a ASGI server called Uvicorn. Turns out when I convert my app from wsgi to asgi (following instructions here https://flask.palletsprojects.com/en/2.0.x/deploying/asgi/) suddently app.logger function is undefined. But I really need the logging functionality to develop my app.

Please follow the steps below to reproduce the problem.

I wrote this basic app

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)

I installed uvicorn

pip install uvicorn

Ran my app

uvicorn app:app

And I get this error when the app tries to log using 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'

Solution

  • An instance of Flask supports the WSGI protocol, and it also adds a number of conveniences, such as a logger. If you wrap the Flask instance and lose the reference to it, you lose those convenience methods.

    Instead of

    app = WsgiToAsgi(app)
    

    try

    wsgi = WsgiToAsgi(app)
    

    then

    uvicorn app:wsgi