Bases: Formatter
flowchart TD
logging.formatter.colored_console.ColoredConsoleFormatter[ColoredConsoleFormatter]
click logging.formatter.colored_console.ColoredConsoleFormatter href "" "logging.formatter.colored_console.ColoredConsoleFormatter"
Formatter to support ANSI color codes and print colorful logs to the console.
Methods:
Attributes:
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)
|
MAPPING: dict[str, Graphic] = {'DEBUG': grey, 'INFO': default, 'WARNING': yellow, 'ERROR': red, 'CRITICAL': black + red}
ANSI Code mapping for each log level.
__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")
|
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)
|