gouge.uvicorn

Helpers for running gouge’s formatters under uvicorn.

This module is named uvicorn but never imports the uvicorn package (all imports in gouge are absolute, so import uvicorn elsewhere still finds the real one).

gouge.uvicorn.UVICORN_LOGGERS = ('uvicorn', 'uvicorn.error', 'uvicorn.access')

The loggers capture_uvicorn() redirects by default.

gouge.uvicorn.capture_uvicorn(level=None, loggers=('uvicorn', 'uvicorn.error', 'uvicorn.access'))

Route uvicorn’s log records through the root handlers.

uvicorn installs its own handlers on its loggers and stops them from propagating, so the formatter configured on the root logger (for example with basicConfig()) never sees them. This removes the handlers of loggers and enables propagation. uvicorn is not imported.

Call it after uvicorn configured its loggers, or uvicorn’s setup undoes it: either call it in a startup hook of the application (for example the FastAPI lifespan), or start uvicorn with log_config=None so uvicorn does not install handlers in the first place (then only the propagate flags matter). Note that access_log=False makes uvicorn disable propagation of uvicorn.access; calling this afterwards enables it again.

Parameters:
level=None

Optional level to set on loggers.

loggers=('uvicorn', 'uvicorn.error', 'uvicorn.access')

Names of the loggers to redirect. Defaults to uvicorn, uvicorn.error and uvicorn.access.

Return type:

None