Skip to content

Logging config

logging_config #

LoggingManager #

LoggingManager(
    name: str, base_dir: pathlib.Path | None = None
)

Manages the configuration and initialization of a structured logger.

This class provides flexible options for configuring log levels, formats, and output destinations.

Examples:

Initialize with default settings: >>> manager = LoggingManager(name="mypackage") >>> logger = manager.get_logger() >>> logger.info("Info message")

Methods:

Name Description
configure_logging

Dynamically adjust logging settings.

get_logger

Retrieve the logger instance.

Source code in src/imgtools/loggers/logging_config.py
def __init__(
    self,
    name: str,
    base_dir: Path | None = None,
) -> None:
    self.name = name
    self.base_dir = base_dir or Path.cwd()
    self.level = self.env_level
    self.enable_json_logging = (
        os.environ.get(f"{self.name}_enable_json_logging".upper(), "0")
        == "1"
    )
    self._initialize_logger()

base_logging_config property #

base_logging_config: typing.Dict

Create the basic logging configuration settings.

Returns:

Type Description
dict

Base logging configuration.

configure_logging #

configure_logging(
    level: str = imgtools.loggers.logging_config.DEFAULT_LOG_LEVEL,
) -> structlog.stdlib.BoundLogger

Dynamically adjust logging settings.

Parameters:

Name Type Description Default
level #
str

Set the log level.

imgtools.loggers.logging_config.DEFAULT_LOG_LEVEL

Returns:

Type Description
structlog.stdlib.BoundLogger

Updated logger instance.

Source code in src/imgtools/loggers/logging_config.py
def configure_logging(
    self, level: str = DEFAULT_LOG_LEVEL
) -> structlog.stdlib.BoundLogger:
    """
    Dynamically adjust logging settings.

    Parameters
    ----------
    level : str, optional
        Set the log level.

    Returns
    -------
    structlog.stdlib.BoundLogger
        Updated logger instance.

    Raises
    ------
    ValueError
        If an invalid log level is specified.
    """
    level_upper = level.upper()
    if level_upper not in VALID_LOG_LEVELS:
        msg = f"Invalid logging level: {level}"
        raise ValueError(msg)

    # Store the old level for logging the change
    self.level = level_upper

    self._initialize_logger()
    logger = self.get_logger()

    return logger

get_logger #

get_logger() -> structlog.stdlib.BoundLogger

Retrieve the logger instance.

Returns:

Type Description
structlog.stdlib.BoundLogger

Configured logger instance.

Source code in src/imgtools/loggers/logging_config.py
def get_logger(self) -> structlog.stdlib.BoundLogger:
    """
    Retrieve the logger instance.

    Returns
    -------
    structlog.stdlib.BoundLogger
        Configured logger instance.
    """
    return structlog.get_logger(self.name)