structlog_config.fastapi_access_logger#
Requires fastapi and is not loaded by default since fastapi is not a default dependency.
Attributes#
Functions#
|
Generate a descriptive route name for timing metrics |
|
Get the URL with the substitution of query parameters. |
|
Get the client IP address from the request. |
|
Check if the request is for static assets. Pretty naive check. |
|
Add better access logging to fastapi: |
Module Contents#
- structlog_config.fastapi_access_logger.get_route_name(app: fastapi.FastAPI, scope: starlette.types.Scope, prefix: str = '') str[source]#
Generate a descriptive route name for timing metrics
- structlog_config.fastapi_access_logger.get_path_with_query_string(scope: starlette.types.Scope) str[source]#
Get the URL with the substitution of query parameters.
- Parameters:
scope (Scope) – Current context.
- Returns:
URL with query parameters
- Return type:
- structlog_config.fastapi_access_logger.client_ip_from_request(request: starlette.requests.Request | starlette.websockets.WebSocket) str | None[source]#
Get the client IP address from the request.
Uses fastapi-ipware library to properly extract client IP from various proxy headers. Fallback to direct client connection if no proxy headers found.
- structlog_config.fastapi_access_logger.is_static_assets_request(scope: starlette.types.Scope) bool[source]#
Check if the request is for static assets. Pretty naive check.
- Parameters:
scope (Scope) – Current context.
- Returns:
True if the request is for static assets, False otherwise.
- Return type:
- structlog_config.fastapi_access_logger.add_middleware(app: fastapi.FastAPI) None[source]#
Add better access logging to fastapi:
>>> from structlog_config import fastapi_access_logger >>> fastapi_access_logger.add_middleware(app)
You’ll also want to disable the default uvicorn logs:
>>> uvicorn.run(..., log_config=None, access_log=False)