Logging

Structured logging configuration and utilities.

Module Contents

Structured logging configuration using structlog.

configure_structlog(log_level='INFO', *, use_json=False)[source]

Configure structlog for the application.

Parameters:
  • log_level (str) – Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)

  • use_json (bool) – If True, use JSON formatter for production. If False, use rich console for development.

Returns:

Configured structlog plugin

Return type:

StructlogPlugin

get_logger(name=None)[source]

Get a structlog logger instance.

Parameters:

name (str | None) – Logger name. If None, uses the root logger.

Return type:

Any

Returns:

A structlog logger instance

bind_correlation_id(correlation_id)[source]

Bind a correlation ID to the current context.

Parameters:

correlation_id (str) – Unique identifier to correlate related log entries

Return type:

None

unbind_correlation_id()[source]

Remove the correlation ID from the current context.

Return type:

None

bind_user_context(user_id, username=None)[source]

Bind user context to the current logging context.

Parameters:
  • user_id (int | str) – User identifier

  • username (str | None) – Username (optional)

Return type:

None

unbind_user_context()[source]

Remove user context from the current logging context.

Return type:

None