Skip to content

Logging

Logging configuration management for consistent log formatting across services.

Overview

The logging module provides utilities to configure Python's logging system using the bundled log-config.toml file. All Imbi services use the same logging configuration for consistency.

Basic Usage

from imbi_common import logging

# Configure logging with bundled config
logging.configure_logging()

# Enable DEBUG level for development
logging.configure_logging(dev=True)

# Use custom config
custom_config = {
    "version": 1,
    "formatters": {
        "simple": {
            "format": "%(levelname)s: %(message)s"
        }
    },
    "handlers": {
        "console": {
            "class": "logging.StreamHandler",
            "formatter": "simple"
        }
    },
    "root": {
        "level": "INFO",
        "handlers": ["console"]
    }
}
logging.configure_logging(log_config=custom_config)

Log Configuration

The bundled log-config.toml provides: - Structured log formatting with timestamps - Separate handlers for console and file output - Appropriate log levels for different components - Rotating file handlers with size limits

Development Mode

When dev=True is passed to configure_logging(), all loggers under the imbi namespace are automatically set to DEBUG level. This is useful for local development.

API Reference

get_log_config

get_log_config() -> _LoggingConfig

Load logging configuration from bundled log-config.toml.

Returns:

Name Type Description
dict _LoggingConfig

Logging configuration dictionary suitable for logging.dictConfig()

Source code in src/imbi_common/logging.py
def get_log_config() -> _LoggingConfig:
    """Load logging configuration from bundled log-config.toml.

    Returns:
        dict: Logging configuration dictionary suitable for
            logging.dictConfig()

    """
    log_config_file = resources.files('imbi_common') / 'log-config.toml'
    return typing.cast(
        _LoggingConfig,
        typing.cast(object, tomllib.loads(log_config_file.read_text())),
    )

configure_logging

configure_logging(
    log_config: _LoggingConfig | None = None,
    dev: bool = False,
) -> None

Configure logging using dictConfig.

Parameters:

Name Type Description Default
log_config _LoggingConfig | None

Optional logging config dict. If None, loads from log-config.toml

None
dev bool

If True, sets imbi logger to DEBUG level

False
Source code in src/imbi_common/logging.py
def configure_logging(
    log_config: _LoggingConfig | None = None, dev: bool = False
) -> None:
    """Configure logging using dictConfig.

    Args:
        log_config: Optional logging config dict. If None, loads from
            log-config.toml
        dev: If True, sets imbi logger to DEBUG level

    """
    if log_config is None:
        log_config = get_log_config()

    if dev:
        loggers = log_config.setdefault('loggers', {})
        loggers.setdefault('imbi', {})
        loggers['imbi']['level'] = 'DEBUG'

    config.dictConfig(log_config)  # type: ignore[arg-type]