1

I am trying to enable all requests to be logged (to centralized logging system) in a Quart microservice. However this only occurs when running directly in Python, and when running in Hypercorn it only logs major events and errors.

Running from PyCharm does generate the logs (to console, and to centralized log):

# TryLogging.py
import logging

from quart import Quart

app = Quart(__name__)
app.logger.setLevel(logging.INFO)


@app.route("/")
def callme():
    return "I'm alive!"

@app.route("/fake_fail")
def failme():
    raise Exception("Fake exception")


if __name__ == "__main__":
    app.run()

generates console logs:

 * Serving Quart app 'TryLogging'
 * Environment: production
 * Please use an ASGI server (e.g. Hypercorn) directly in production
 * Debug mode: False
 * Running on http://127.0.0.1:5000 (CTRL + C to quit)
[2022-01-10 15:55:48,323] Running on http://127.0.0.1:5000 (CTRL + C to quit)
[2022-01-10 15:55:50,080] 127.0.0.1:63560 GET / 1.1 200 10 4515
[2022-01-10 15:55:54,480] 127.0.0.1:63560 GET /fake_fail 1.1 500 290 1999
[2022-01-10 15:55:54,478] ERROR in app: Exception on request GET /fake_fail
Traceback (most recent call last):
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1489, in handle_request
    return await self.full_dispatch_request(request_context)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1514, in full_dispatch_request
    result = await self.handle_user_exception(error)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 964, in handle_user_exception
    raise error
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1512, in full_dispatch_request
    result = await self.dispatch_request(request_context)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1557, in dispatch_request
    return await self.ensure_async(handler)(**request_.view_args)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\utils.py", line 66, in _wrapper
    result = await loop.run_in_executor(
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\brend\Documents\GitHub\ms-abs-boundaries\src\TryLogging.py", line 15, in failme
    raise Exception("Fake exception")
Exception: Fake exception

However when running through Hypercorn in terminal (as it launched in production) and calling the endpoint from browser:

(ms-abs-boundaries) PS C:\Users\brend\Documents\GitHub\ms-abs-boundaries\src> hypercorn --bind 127.0.0.1:5008 TryLoggi
ng.py
[2022-01-10 15:56:42 +1100] [37772] [INFO] Running on http://127.0.0.1:5008 (CTRL + C to quit)
[2022-01-10 15:56:48,075] ERROR in app: Exception on request GET /fake_fail
Traceback (most recent call last):
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1489, in handle_request

    return await self.full_dispatch_request(request_context)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1514, in full_dispatch_
request
    result = await self.handle_user_exception(error)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 964, in handle_user_exc
eption
    raise error
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1512, in full_dispatch_
request
    result = await self.dispatch_request(request_context)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\app.py", line 1557, in dispatch_reque
st
    return await self.ensure_async(handler)(**request_.view_args)
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\site-packages\quart\utils.py", line 66, in _wrapper
    result = await loop.run_in_executor(
  File "C:\Users\brend\miniconda3\envs\ms-abs-boundaries\lib\concurrent\futures\thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
  File "C:\Users\brend\Documents\GitHub\ms-abs-boundaries\src\TryLogging.py", line 15, in failme
    raise Exception("Fake exception")
Exception: Fake exception


Only the exception is logged and the other success request is not logged.

How can I enable all requests (including other arbitrary info log events) to be logged when running in Hypercorn?

Hypercorn version: 0.13.2
Quart version: 0.16.2

NOTE: it needs to write to an external log system, not local logging file, and that external log is configured in the real version of the app. But getting it to show in the console is enough for testing.

Brendan Hill
  • 3,406
  • 4
  • 32
  • 61

1 Answers1

1

Logs emitted by your application code would still be logged. What differs when you move from running with Quart to Hypercorn is that you no longer have the quart.serving logger which produced those messages you referred to.

To get similar behavior with Hypercorn, you can configure it to direct access log to stdout by setting accesslog to -.

nirvana-msu
  • 3,877
  • 2
  • 19
  • 28