Skip to content

logging ¤

Logging module.

Classes:

Logger ¤

Static class to store loggers.

Methods:

get_logger staticmethod ¤

get_logger(
    name: str,
    level: int | None = None,
    fmt: str = ":%(lineno)d: %(message)s",
) -> Logger

Return a logger.

Parameters:

  • name (str) –

    Name to pass to the logging module.

  • level (int | None, default: None ) –

    Level of logging.

  • fmt (str, default: ':%(lineno)d: %(message)s' ) –

    Format string.

Returns:

  • Logger

    Logger from logging.getLogger.

Source code in src/archan/logging.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@staticmethod
def get_logger(name: str, level: int | None = None, fmt: str = ":%(lineno)d: %(message)s") -> logging.Logger:
    """Return a logger.

    Parameters:
        name: Name to pass to the logging module.
        level: Level of logging.
        fmt: Format string.

    Returns:
        Logger from ``logging.getLogger``.
    """
    if name not in Logger.loggers:
        if Logger.level is None and level is None:
            Logger.level = logging.ERROR
            level = logging.ERROR
        elif Logger.level is None:
            Logger.level = level
        elif level is None:
            level = Logger.level
        logger = logging.getLogger(name)
        logger_handler = logging.StreamHandler()
        logger_handler.setFormatter(LoggingFormatter(fmt=name + fmt))
        logger.addHandler(logger_handler)
        logger.setLevel(level)  # type: ignore[arg-type]
        Logger.loggers[name] = logger
    return Logger.loggers[name]

set_level staticmethod ¤

set_level(level: int) -> None

Set level of logging for all loggers.

Parameters:

  • level (int) –

    Level of logging.

Source code in src/archan/logging.py
17
18
19
20
21
22
23
24
25
26
@staticmethod
def set_level(level: int) -> None:
    """Set level of logging for all loggers.

    Parameters:
        level: Level of logging.
    """
    Logger.level = level
    for logger in Logger.loggers.values():
        logger.setLevel(level)

LoggingFormatter ¤

Bases: Formatter

Custom logging formatter.

Methods:

  • format

    Override default format method.

format ¤

format(record: LogRecord) -> str

Override default format method.

Parameters:

Returns:

  • str

    The formatted record.

Source code in src/archan/logging.py
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def format(self, record: logging.LogRecord) -> str:  # noqa: A003
    """Override default format method.

    Parameters:
        record: A log record.

    Returns:
        The formatted record.
    """
    if record.levelno == logging.DEBUG:
        string = Back.WHITE + Fore.BLACK + " debug "
    elif record.levelno == logging.INFO:
        string = Back.BLUE + Fore.WHITE + " info "
    elif record.levelno == logging.WARNING:
        string = Back.YELLOW + Fore.BLACK + " warning "
    elif record.levelno == logging.ERROR:
        string = Back.RED + Fore.WHITE + " error "
    elif record.levelno == logging.CRITICAL:
        string = Back.BLACK + Fore.WHITE + " critical "
    else:
        string = ""
    return f"{Style.RESET_ALL}{string}{Style.RESET_ALL} {super().format(record)}"