Skip to content
Night Brownie Night Brownie

Logging info

Logging processors for structlog.

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

Classes:

  • Development

    The development logging configuration.

  • LoggerConfig

    The configuration of a logger.

  • Logging

    Customized implementation inspired by the following documentation.

  • Production

    The production logging configuration.

Functions:

  • 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.

Development

Bases: Logging[ConsoleRenderer]

The development logging configuration.

Methods:

  • get_renderer

    Return the console renderer for the development environment.

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.

Parameters:

  • level (NotRequired[str]) –
  • propagate (NotRequired[bool]) –
  • handlers (NotRequired[list[str]]) –
  • extra (NotRequired[dict]) –

Parameters:

  • level (str, default: ... ) –
  • propagate (bool, default: ... ) –
  • handlers (list[str], default: ... ) –
  • extra (dict, default: ... ) –

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

Methods:

  • 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.

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:

  • 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.

Methods:

  • get_renderer

    Return the JSON renderer for the production environment.

get_renderer classmethod

get_renderer() -> JSONRenderer

Return the JSON renderer for the production environment.

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.