Skip to content

formatter.colored_console

Classes:

Name Description
ColoredConsoleFormatter

Formatter to support ANSI color codes and print colorful logs to the console.

Classes

ColoredConsoleFormatter

Bases: Formatter


              flowchart TD
              formatter.colored_console.ColoredConsoleFormatter[ColoredConsoleFormatter]

              

              click formatter.colored_console.ColoredConsoleFormatter href "" "formatter.colored_console.ColoredConsoleFormatter"
            

Formatter to support ANSI color codes and print colorful logs to the console.

Methods:

Name Description
__init__
format

Attributes:

Name Type Description
MAPPING dict[str, Graphic]

ANSI Code mapping for each log level.

ansi_dict
Source code in dlr/ki/logging/formatter/colored_console.py
class ColoredConsoleFormatter(Formatter):
    """
    Formatter to support ANSI color codes and print colorful logs to the console.
    """

    MAPPING: Dict[str, AnsiGraphic] = {
        "DEBUG":    ansi.fg.grey,
        "INFO":     ansi.fg.default,
        "WARNING":  ansi.fg.yellow,
        "ERROR":    ansi.fg.red,
        "CRITICAL": ansi.fg.black + ansi.bg.red,
    }
    """
    ANSI Code mapping for each log level.
    """

    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

        # create a dict with all ansi functions to provide them while formatting
        self.ansi_dict = {}
        for key, value in ansi.fx.__dict__.items():
            if isinstance(value, AnsiGraphic):
                self.ansi_dict[f"ansi.fx.{key}"] = value
        for key, value in ansi.fg.__dict__.items():
            if isinstance(value, AnsiGraphic):
                self.ansi_dict[f"ansi.fg.{key}"] = value
        for key, value in ansi.bg.__dict__.items():
            if isinstance(value, AnsiGraphic):
                self.ansi_dict[f"ansi.bg.{key}"] = value
        self.ansi_dict.pop("ansi.fg.bold")
        self.ansi_dict.pop("ansi.bg.bold")

    def format(self, record):
        colored_record = copy(record)
        colored_record.levelcolor = ColoredConsoleFormatter.MAPPING.get(
            colored_record.levelname,
            ColoredConsoleFormatter.MAPPING["DEBUG"]
        )
        colored_record.colorreset = ansi.fx.reset
        colored_record.clearline = erase_line(2) + "\r"
        colored_record.__dict__.update(self.ansi_dict)
        return super().format(colored_record)

Attributes

MAPPING class-attribute instance-attribute
MAPPING: dict[str, Graphic] = {'DEBUG': grey, 'INFO': default, 'WARNING': yellow, 'ERROR': red, 'CRITICAL': black + red}

ANSI Code mapping for each log level.

ansi_dict instance-attribute
ansi_dict = {}

Functions

__init__
__init__(*args, **kwargs) -> None
Source code in dlr/ki/logging/formatter/colored_console.py
def __init__(self, *args, **kwargs) -> None:
    super().__init__(*args, **kwargs)

    # create a dict with all ansi functions to provide them while formatting
    self.ansi_dict = {}
    for key, value in ansi.fx.__dict__.items():
        if isinstance(value, AnsiGraphic):
            self.ansi_dict[f"ansi.fx.{key}"] = value
    for key, value in ansi.fg.__dict__.items():
        if isinstance(value, AnsiGraphic):
            self.ansi_dict[f"ansi.fg.{key}"] = value
    for key, value in ansi.bg.__dict__.items():
        if isinstance(value, AnsiGraphic):
            self.ansi_dict[f"ansi.bg.{key}"] = value
    self.ansi_dict.pop("ansi.fg.bold")
    self.ansi_dict.pop("ansi.bg.bold")
format
format(record)
Source code in dlr/ki/logging/formatter/colored_console.py
def format(self, record):
    colored_record = copy(record)
    colored_record.levelcolor = ColoredConsoleFormatter.MAPPING.get(
        colored_record.levelname,
        ColoredConsoleFormatter.MAPPING["DEBUG"]
    )
    colored_record.colorreset = ansi.fx.reset
    colored_record.clearline = erase_line(2) + "\r"
    colored_record.__dict__.update(self.ansi_dict)
    return super().format(colored_record)