|
|
@@ -1,19 +1,30 @@
|
|
|
"""Logging utilities (internal)"""
|
|
|
+
|
|
|
import logging
|
|
|
import time
|
|
|
|
|
|
from typing import Callable, ClassVar, Final
|
|
|
|
|
|
GREY: Final[str] = "\x1b[38;20m"
|
|
|
-BOLD_PURPLE: Final[str] = "\x1b[35;1m"
|
|
|
YELLOW: Final[str] = "\x1b[33;20m"
|
|
|
+BLUE: Final[str] = "\x1b[34;20m"
|
|
|
+CYAN: Final[str] = "\x1b[36;20m"
|
|
|
RED: Final[str] = "\x1b[31;20m"
|
|
|
+
|
|
|
BOLD_RED: Final[str] = "\x1b[31;1m"
|
|
|
+BOLD_PURPLE: Final[str] = "\x1b[35;1m"
|
|
|
+
|
|
|
RESET: Final[str] = "\x1b[0m"
|
|
|
|
|
|
|
|
|
+def colored(code: str | None, fmt: str) -> str:
|
|
|
+ """Apply a given color code"""
|
|
|
+ return f"{code}{fmt}{RESET}" if code else fmt
|
|
|
+
|
|
|
+
|
|
|
class CIFormatter(logging.Formatter):
|
|
|
"""A custom formatter for CI messages"""
|
|
|
+
|
|
|
DEFAULT_FORMAT: Final[ClassVar[str]] = (
|
|
|
"%(asctime)s - %(name)s - %(levelname)s - %(message)s (%(filename)s:%(lineno)d)"
|
|
|
)
|
|
|
@@ -45,9 +56,10 @@ class CIFormatter(logging.Formatter):
|
|
|
self._color_codes = CIFormatter.DEFAULT_COLOR_CODES | colors
|
|
|
|
|
|
def _dispatch(self, level: int) -> logging.Formatter:
|
|
|
- log_fmt: str | None = self._color_codes.get(level)
|
|
|
- log_fmt = self._format if log_fmt is None else f"{log_fmt}{self._format}{RESET}"
|
|
|
- fmt = logging.Formatter(fmt=log_fmt, datefmt=self.datefmt)
|
|
|
+ fmt = logging.Formatter(
|
|
|
+ fmt=colored(self._color_codes.get(level), self._format),
|
|
|
+ datefmt=self.datefmt,
|
|
|
+ )
|
|
|
fmt.converter = self.converter
|
|
|
return fmt
|
|
|
|