Skip to content

Logging info

Logging processors for structlog.

Inspired by: https://ouassim.tech/notes/setting-up-structured-logging-in-fastapi-with-structlog/

CLASS DESCRIPTION
Development

The development logging configuration.

LoggerConfig

The configuration of a logger.

Logging

Customized implementation inspired by the following documentation.

Production

The production logging configuration.

FUNCTION DESCRIPTION
configure

Configure structlog and standard Python logging for structured logging.

drop_color_message_key

Drop the color_message key from the event dict if it exists.

edit_event_name

Edit the event dict to change the event name, so we don't clobber elastic indices.

generate_correlation_id

Generate a value that is unique for each request.

tracer_injection

Inject the current trace and span ids into the event dictionary.

Classes

Development

Bases: Logging[ConsoleRenderer]

The development logging configuration.

METHOD DESCRIPTION
get_renderer

Return the console renderer for the development environment.

Functions

get_renderer classmethod
get_renderer() -> ConsoleRenderer

Return the console renderer for the development environment.

LoggerConfig typed-dict

LoggerConfig(*, level: str = ..., propagate: bool = ..., handlers: list[str] = ..., extra: dict = ...)

Bases: TypedDict

The configuration of a logger.

PARAMETER DESCRIPTION
level

TYPE: NotRequired[str]

propagate

TYPE: NotRequired[bool]

handlers

TYPE: NotRequired[list[str]]

extra

TYPE: NotRequired[dict]

PARAMETER DESCRIPTION
level

TYPE: str DEFAULT: ...

propagate

TYPE: bool DEFAULT: ...

handlers

TYPE: list[str] DEFAULT: ...

extra

TYPE: dict DEFAULT: ...

Functions

Logging

Bases: Generic[RendererType]

Customized implementation inspired by the following documentation.

https://www.structlog.org/en/stable/standard-library.html#rendering-using-structlog-based-formatters-within-logging

METHOD DESCRIPTION
configure

Configure structlog and standard Python logging for structured logging.

configure_stdlib

Configure the standard Python logging module for structured logging.

configure_structlog

Configure structlog with the current settings.

get_processors

Retrieves and constructs a list of processors used for structlog logging.

get_renderer

Return the renderer for structlog logging based on the current environment.

Functions

configure classmethod
configure() -> None

Configure structlog and standard Python logging for structured logging.

configure_stdlib classmethod
configure_stdlib() -> None

Configure the standard Python logging module for structured logging.

configure_structlog classmethod
configure_structlog() -> None

Configure structlog with the current settings.

get_processors classmethod
get_processors() -> list[Any]

Retrieves and constructs a list of processors used for structlog logging.

This method dynamically determines the appropriate processors for the current environment and ensures they are formatted correctly for logging. In production environments, additional processors may be included based on specific requirements.

RETURNS DESCRIPTION
list[Any]

A list of processor instances used for structlog logging.

get_renderer classmethod
get_renderer() -> RendererType

Return the renderer for structlog logging based on the current environment.

Production

Bases: Logging[JSONRenderer]

The production logging configuration.

METHOD DESCRIPTION
get_renderer

Return the JSON renderer for the production environment.

Functions

get_renderer classmethod
get_renderer() -> JSONRenderer

Return the JSON renderer for the production environment.

Functions

configure

configure() -> None

Configure structlog and standard Python logging for structured logging.

drop_color_message_key

drop_color_message_key(_: Any, __: Any, event_dict: EventDict) -> EventDict

Drop the color_message key from the event dict if it exists.

Uvicorn logs the message a second time in the extra color_message, but we don't need it.

edit_event_name

edit_event_name(_: Any, __: Any, event_dict: EventDict) -> EventDict

Edit the event dict to change the event name, so we don't clobber elastic indices.

generate_correlation_id

generate_correlation_id() -> str

Generate a value that is unique for each request.

tracer_injection

tracer_injection(_: Any, __: Any, event_dict: EventDict) -> EventDict

Inject the current trace and span ids into the event dictionary.

This function gets called by structlog as a processor, and the logger and log_method are not used until called by structlog.