Skip to content

loggers

Describe loggers as a class, or get a fully initialized logger with LoggerFactory.

LoggerConfig dataclass

Bases: BaseLoggingConfig

Define a logging Logger.

Parameters:

Name Type Description Default
name str

The name of the logger.

required
level str

The level of log messages this logger should show (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL).

required
handlers list[str]

List of handler names this logger should use. These handlers must exist in the logging dictConfig.

required
propagate bool

If True, messages will be propagated up/down to the root logger.

False
Source code in src/red_logging/config_classes/loggers/_loggers.py
@dataclass
class LoggerConfig(BaseLoggingConfig):
    """Define a logging Logger.

    Params:
        name (str): The name of the logger.
        level (str): The level of log messages this logger should show (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL).
        handlers (list[str]): List of handler names this logger should use. These handlers must exist in the logging dictConfig.
        propagate (bool): If `True`, messages will be propagated up/down to the root logger.
    """

    name: str
    level: str
    handlers: list[str]
    propagate: bool = False

    def get_configdict(self) -> dict:
        """Return a dict representation of the logger described by this class."""
        logger_dict: dict[str, dict[str, t.Any]] = {
            self.name: {
                "level": self.level,
                "handlers": self.handlers,
                "propagate": self.propagate,
            }
        }
        return logger_dict

get_configdict()

Return a dict representation of the logger described by this class.

Source code in src/red_logging/config_classes/loggers/_loggers.py
def get_configdict(self) -> dict:
    """Return a dict representation of the logger described by this class."""
    logger_dict: dict[str, dict[str, t.Any]] = {
        self.name: {
            "level": self.level,
            "handlers": self.handlers,
            "propagate": self.propagate,
        }
    }
    return logger_dict

LoggerFactory

Generate loggers based on LoggerFactory's config.

Source code in src/red_logging/config_classes/loggers/_factory.py
class LoggerFactory:
    """Generate loggers based on LoggerFactory's config."""

    _LOG: logging.Logger | None = None

    @staticmethod
    def __create_logger(
        name: str,
        log_level: str,
        handlers: dict[str, dict],
        formatters: dict[str, dict],
        loggers: dict[str, dict],
    ) -> logging.Logger:
        """Create a logger cnofig from inputs.

        Params:
            name (str): The name of the logger.
            log_level (str): The log levels to show (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL).
            handlers (dict[str, dict[str, Any]]): A dict describing the handlers for this logger config.
            formatters (dict[str, dict[str, Any]]): A dict describing the formatters for this logger config.
            loggers (dict[str, dict[str, Any]]): A dict describing the loggers for this logger config.
        """
        log_level = log_level.upper()

        # Configure logging using dictConfig
        logging_config = {
            "version": 1,
            "handlers": handlers,
            "formatters": formatters,
            "loggers": loggers,
            "root": {
                "level": log_level,
                "handlers": list(handlers.keys()),
            },
        }

        try:
            logging.config.dictConfig(logging_config)
        except Exception as exc:
            msg = Exception(f"Unhandled exception configuring logger. Details: {exc}")
            # log.error(msg)

            raise msg

        # Get or create logger
        LoggerFactory._LOG = logging.getLogger(name)

        return LoggerFactory._LOG

    @staticmethod
    def get_logger(
        name: str,
        log_level: str,
        handlers: dict[str, dict],
        formatters: dict[str, dict],
        loggers: dict[str, dict],
    ) -> logging.Logger:
        """Initialize a logger.

        Params:
            name (str): The name of the logger.
            log_level (str): The log levels to show (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL).
            handlers (dict[str, dict[str, Any]]): A dict describing the handlers for this logger config.
            formatters (dict[str, dict[str, Any]]): A dict describing the formatters for this logger config.
            loggers (dict[str, dict[str, Any]]): A dict describing the loggers for this logger config.
        """
        logger = LoggerFactory.__create_logger(
            name=name,
            log_level=log_level,
            handlers=handlers,
            formatters=formatters,
            loggers=loggers,
        )

        return logger

get_logger(name, log_level, handlers, formatters, loggers) staticmethod

Initialize a logger.

Parameters:

Name Type Description Default
name str

The name of the logger.

required
log_level str

The log levels to show (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL).

required
handlers dict[str, dict[str, Any]]

A dict describing the handlers for this logger config.

required
formatters dict[str, dict[str, Any]]

A dict describing the formatters for this logger config.

required
loggers dict[str, dict[str, Any]]

A dict describing the loggers for this logger config.

required
Source code in src/red_logging/config_classes/loggers/_factory.py
@staticmethod
def get_logger(
    name: str,
    log_level: str,
    handlers: dict[str, dict],
    formatters: dict[str, dict],
    loggers: dict[str, dict],
) -> logging.Logger:
    """Initialize a logger.

    Params:
        name (str): The name of the logger.
        log_level (str): The log levels to show (NOTSET, DEBUG, INFO, WARNING, ERROR, CRITICAL).
        handlers (dict[str, dict[str, Any]]): A dict describing the handlers for this logger config.
        formatters (dict[str, dict[str, Any]]): A dict describing the formatters for this logger config.
        loggers (dict[str, dict[str, Any]]): A dict describing the loggers for this logger config.
    """
    logger = LoggerFactory.__create_logger(
        name=name,
        log_level=log_level,
        handlers=handlers,
        formatters=formatters,
        loggers=loggers,
    )

    return logger