Skip to main content

portia.logger

Logging functions for managing and configuring loggers.

This module defines functions and classes to manage logging within the application. It provides a LoggerManager class that manages the package-level logger and allows customization. The LoggerInterface defines the general interface for loggers, and the default logger is provided by loguru. The logger function returns the active logger, and the LoggerManager can be used to configure logging behavior.

Classes in this file include:

  • LoggerInterface: A protocol defining the common logging methods (debug, info, warning, error, LoggerInterface0).
  • LoggerManager: A class for managing the logger, allowing customization and configuration from the application's settings.

This module ensures flexible and configurable logging, supporting both default and custom loggers.

LoggerInterface Objects

class LoggerInterface(Protocol)

General Interface for loggers.

This interface defines the common methods that any logger should implement. The methods are:

  • debug: For logging debug-level messages.
  • info: For logging informational messages.
  • warning: For logging warning messages.
  • error: For logging error messages.
  • critical: For logging critical error messages.

These methods are used throughout the application for logging messages at various levels.

Formatter Objects

class Formatter()

A class used to format log records.

Attributes

max_lines : int The maximum number of lines to include in the formatted log message.

Methods

format(record) Formats a log record into a string.

__init__

def __init__() -> None

Initialize the logger with default settings.

Attributes:

  • max_lines int - The maximum number of lines the logger can handle, default is 30.

format

def format(record: Any) -> str

Format a log record into a string with specific formatting.

Arguments:

  • record dict - A dictionary containing log record information. Expected keys are "message", "extra", "time", "level", "name", "function", and "line".

Returns:

  • str - The formatted log record string.

_sanitize_message_

def _sanitize_message_(msg: str) -> str

Sanitize a message to be used in a log record.

_get_function_color_

def _get_function_color_(record: Any) -> str

Get color based on function/module name. Default is white.

LoggerManager Objects

class LoggerManager()

Manages the package-level logger.

The LoggerManager is responsible for initializing and managing the logger used throughout the application. It provides functionality to configure the logger, set a custom logger, and adjust logging settings based on the application's configuration.

Arguments:

  • custom_logger LoggerInterface | None - A custom logger to be used. If not provided, the default loguru logger will be used.

Attributes:

  • logger LoggerInterface - The current active logger.
  • custom_logger bool - A flag indicating whether a custom logger is in use.

Methods:

  • logger - Returns the active logger.
  • set_logger - Sets a custom logger.
  • configure_from_config - Configures the logger based on the provided configuration.

__init__

def __init__(custom_logger: LoggerInterface | None = None) -> None

Initialize the LoggerManager.

Arguments:

  • custom_logger LoggerInterface | None - A custom logger to use. Defaults to None.

logger

@property
def logger() -> LoggerInterface

Get the current logger.

Returns:

  • LoggerInterface - The active logger being used.

set_logger

def set_logger(custom_logger: LoggerInterface) -> None

Set a custom logger.

Arguments:

  • custom_logger LoggerInterface - The custom logger to be used.

configure_from_config

def configure_from_config(config: Config) -> None

Configure the global logger based on the library's configuration.

This method configures the logger's log level and output sink based on the application's settings. If a custom logger is in use, it will skip the configuration and log a warning.

Arguments:

  • config Config - The configuration object containing the logging settings.

logger

def logger() -> LoggerInterface

Return the active logger.

Returns:

  • LoggerInterface - The current active logger being used.