Skip to content

clabe.logging

add_file_handler

add_file_handler(
    logger: TLogger, output_path: PathLike
) -> TLogger

Adds a file handler to the logger to write logs to a file.

Creates a new file handler with UTC timezone formatting and adds it to the specified logger for persistent log storage.

Parameters:

Name Type Description Default
logger TLogger

The logger to which the file handler will be added

required
output_path PathLike

The path to the log file

required

Returns:

Name Type Description
TLogger TLogger

The logger with the added file handler

Source code in src/clabe/logging/_stdlib.py
def add_file_handler(logger: TLogger, output_path: os.PathLike) -> TLogger:
    """
    Adds a file handler to the logger to write logs to a file.

    Creates a new file handler with UTC timezone formatting and adds it to the
    specified logger for persistent log storage.

    Args:
        logger: The logger to which the file handler will be added
        output_path: The path to the log file

    Returns:
        TLogger: The logger with the added file handler
    """
    file_handler = logging.FileHandler(Path(output_path), encoding="utf-8", mode="w")
    file_handler.setFormatter(utc_formatter)
    logger.addHandler(file_handler)
    return logger

close_file_handlers

close_file_handlers(logger: TLogger) -> TLogger

Closes all file handlers associated with the logger.

Iterates through all handlers associated with the logger and closes any file handlers to ensure proper resource cleanup.

Parameters:

Name Type Description Default
logger TLogger

The logger whose file handlers will be closed

required

Returns:

Name Type Description
TLogger TLogger

The logger with closed file handlers

Source code in src/clabe/logging/_stdlib.py
def close_file_handlers(logger: TLogger) -> TLogger:
    """
    Closes all file handlers associated with the logger.

    Iterates through all handlers associated with the logger and closes any
    file handlers to ensure proper resource cleanup.

    Args:
        logger: The logger whose file handlers will be closed

    Returns:
        TLogger: The logger with closed file handlers
    """
    for handler in logger.handlers:
        if isinstance(handler, logging.FileHandler):
            handler.close()
    return logger

configure_console_logging

configure_console_logging() -> None

Installs clabe's default root-logger configuration (console handler, format, level).

Source code in src/clabe/logging/_stdlib.py
def configure_console_logging() -> None:
    """
    Installs clabe's default root-logger configuration (console handler, format, level).
    """
    logging.basicConfig(level=logging.INFO, format=log_fmt, datefmt=datetime_fmt, handlers=[rich_handler])

set_console_level

set_console_level(level: int) -> None

Sets the verbosity threshold of the interactive console log handler.

This only affects what is shown to the user on the console; it is fully decoupled from what is written to the log file (see add_file_handler) and from any remote handlers.

Parameters:

Name Type Description Default
level int

A standard logging level (e.g. logging.INFO).

required
Source code in src/clabe/logging/_stdlib.py
def set_console_level(level: int) -> None:
    """
    Sets the verbosity threshold of the interactive console log handler.

    This only affects what is shown to the user on the console; it is fully
    decoupled from what is written to the log file (see ``add_file_handler``)
    and from any remote handlers.

    Args:
        level: A standard ``logging`` level (e.g. ``logging.INFO``).
    """
    rich_handler.setLevel(level)

shutdown_logger

shutdown_logger(logger: TLogger) -> TLogger

Shuts down the logger by closing all file handlers and calling logging.shutdown().

Performs a complete shutdown of the logging system, ensuring all file handlers are properly closed and resources are released.

Parameters:

Name Type Description Default
logger TLogger

The logger to shut down

required

Returns:

Name Type Description
TLogger TLogger

The logger with closed file handlers

Source code in src/clabe/logging/_stdlib.py
def shutdown_logger(logger: TLogger) -> TLogger:
    """
    Shuts down the logger by closing all file handlers and calling logging.shutdown().

    Performs a complete shutdown of the logging system, ensuring all file handlers
    are properly closed and resources are released.

    Args:
        logger: The logger to shut down

    Returns:
        TLogger: The logger with closed file handlers
    """
    close_file_handlers(logger)
    logging.shutdown()
    return logger