소스 검색

refactor: clean up logging code a bit

Sam Jaffe 1 개월 전
부모
커밋
ca1a3db212
1개의 변경된 파일16개의 추가작업 그리고 4개의 파일을 삭제
  1. 16 4
      src/cipy/_logging.py

+ 16 - 4
src/cipy/_logging.py

@@ -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