Skip to content

controller

Logging config for the application.

Ensures that the app, sqlalchemy, saq and uvicorn loggers all log through the queue listener.

Adds a filter for health check route logs.

BeforeSendHandler

BeforeSendHandler()

Extraction of request and response data from connection scope.

__call__ async

__call__(message, _, scope)

Receives ASGI response messages and scope, and logs per configuration.

Parameters:

Name Type Description Default
message Message

ASGI response event.

required
scope Scope

ASGI connection scope.

required

extract_request_data async

extract_request_data(request)

Create a dictionary of values for the log.

Parameters:

Name Type Description Default
request Request

A Request instance.

required

Returns:

Type Description
dict[str, Any]

An OrderedDict.

extract_response_data

extract_response_data(scope)

Extract data from the response.

Parameters:

Name Type Description Default
scope Scope

The ASGI connection scope.

required

Returns:

Type Description
dict[str, Any]

An OrderedDict.

log_request async

log_request(scope)

Handle extracting the request data and logging the message.

Parameters:

Name Type Description Default
scope Scope

The ASGI connection scope.

required

Returns:

Type Description
None

None

log_response async

log_response(scope)

Handle extracting the response data and logging the message.

Parameters:

Name Type Description Default
scope Scope

The ASGI connection scope.

required

Returns:

Type Description
None

None

drop_health_logs

drop_health_logs(_, __, event_dict)

Prevent logging of successful health checks.

Parameters:

Name Type Description Default
_ WrappedLogger

Wrapped logger object.

required
__ str

Name of the wrapped method, e.g., "info", "warning", etc.

required
event_dict EventDict

Current context with current event, e.g, {"a": 42, "event": "foo"}.

required

Returns:

Type Description
EventDict

event_dict for further processing if it does not represent a successful health check.

middleware_factory

middleware_factory(app)

Middleware to ensure that every request has a clean structlog context.

Parameters:

Name Type Description Default
app ASGIApp

The previous ASGI app in the call chain.

required

Returns:

Type Description
ASGIApp

A new ASGI app that cleans the structlog contextvars.