Skip to content

logger ¤

This module contains logging utilities.

We provide the patch_loggers function so dependant libraries can patch loggers as they see fit.

For example, to fit in the MkDocs logging configuration and prefix each log message with the module name:

import logging
from markdown_exec.logger import patch_loggers


class LoggerAdapter(logging.LoggerAdapter):
    def __init__(self, prefix, logger):
        super().__init__(logger, {})
        self.prefix = prefix

    def process(self, msg, kwargs):
        return f"{self.prefix}: {msg}", kwargs


def get_logger(name):
    logger = logging.getLogger(f"mkdocs.plugins.{name}")
    return LoggerAdapter(name.split(".", 1)[0], logger)


patch_loggers(get_logger)

Functions:

get_logger ¤

get_logger(name: str) -> _Logger

Create and return a new logger instance.

Parameters:

  • name (str) –

    The logger name.

Returns:

  • _Logger

    The logger.

patch_loggers ¤

patch_loggers(
    get_logger_func: Callable[[str], Any]
) -> None

Patch loggers.

Parameters:

  • get_logger_func (Callable[[str], Any]) –

    A function accepting a name as parameter and returning a logger.