Skip to content

Logging

leadr.logging

Structured logging configuration using structlog.

This module configures structlog for the LEADR application with support for:

  • JSON output for production (machine-parseable)
  • Colored console output for development (human-readable)
  • File logging with rotation
  • Integration with standard library logging (captures third-party logs)

Functions:

leadr.logging.bind_contextvars

bind_contextvars(**kwargs)

Bind context variables that will be included in all subsequent log entries.

This is useful for adding request-scoped context like request_id that should appear in all logs within that context.

Parameters:

  • **kwargs (Any) – Key-value pairs to bind to the context
Example > > > from leadr.logging import bind_contextvars > > > bind_contextvars(request_id="req-123", user_id="user-456") > > > > > > # All subsequent logs will include request_id and user_id

leadr.logging.clear_contextvars

clear_contextvars()

Clear all bound context variables.

Call this at the start of a new request to ensure no context leaks between requests.

leadr.logging.get_logger

get_logger(name)

Get a structlog logger instance.

Parameters:

  • name (str) – Logger name, typically name of the calling module

Returns:

  • BoundLogger – A bound structlog logger that can be used for structured logging
Example > > > from leadr.logging import get_logger > > > logger = get_logger(__name__) > > > logger.info("User created", user_id="abc-123")

leadr.logging.setup_logging

setup_logging(*, log_level='INFO', json_format=True, log_to_file=False, log_dir=Path('/var/log/leadr'), app_name='LEADR', env='PROD')

Configure structlog for the application.

This function sets up structured logging using structlog with integration to the standard library logging module. This allows both structlog loggers and standard library loggers (used by third-party packages like uvicorn) to be processed through the same pipeline.

Parameters:

  • log_level (str) – Logging level (DEBUG, INFO, WARNING, ERROR)
  • json_format (bool) – Use JSON format (True) or colored console (False)
  • log_to_file (bool) – Enable file logging in addition to stdout
  • log_dir (Path) – Directory for log files when log_to_file is enabled
  • app_name (str) – Application name added to each log entry
  • env (str) – Environment name added to each log entry