Solution / Fix
Now, when you execute uvicorn by the in-Python command uvicorn.run(app)
, this is your next move:
take the ucivorn default logging config and add the handler from your application to it:
config = {}
# this is default (site-packages\uvicorn\main.py)
config['log_config'] = "{
"version":1,
"disable_existing_loggers":true,
"formatters":{
"default":{
"()":"uvicorn.logging.DefaultFormatter",
"fmt":"%(levelprefix)s %(message)s",
"use_colors":"None"
},
"access":{
"()":"uvicorn.logging.AccessFormatter",
"fmt":"%(levelprefix)s %(client_addr)s - \"%(request_line)s\" %(status_code)s"
}
},
"handlers":{
"default":{
"formatter":"default",
"class":"logging.StreamHandler",
"stream":"ext://sys.stderr"
},
"access":{
"formatter":"access",
"class":"logging.StreamHandler",
"stream":"ext://sys.stdout"
}
},
"loggers":{
"uvicorn":{
"handlers":[
"default"
],
"level":"INFO"
},
"uvicorn.error":{
"level":"INFO",
"handlers":[
"default"
],
"propagate":true
},
"uvicorn.access":{
"handlers":[
"access"
],
"level":"INFO",
"propagate":false
}
}
}
# add your handler to it (in my case, I'm working with quart, but you can do this with Flask etc. as well, they're all the same)
config['log_config']['loggers']['quart'] =
{
"handlers":[
"default"
],
"level":"INFO"
}
this will keep the logger from quart/Flask/etc. enabled when uvicorn starts. Alternatively, you can set disable_existing_loggers
to False. But this will keep all loggers enabled and then you will probable get more messages than you wish.
Finally, pass the config to uvicorn:
uvicorn.run(app, **config)
Explanation
When uvicorn's logging config has set disable_existing_loggers
to True, all other loggers will be disabled. This also means that the logger quart and Flask use (which prints the traceback) get disabled. You can either set the config to NOT disable other loggers, or re-add them to the config so uvicorn doesn't disable them in the first place.