MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / ColoredFormatter

Class ColoredFormatter

tools/logger_factory.py:16–39  ·  view source on GitHub ↗

自定义格式化器,根据日志级别为日志消息添加颜色。 INFO: 绿色 WARNING: 黄色 ERROR: 红色

Source from the content-addressed store, hash-verified

14 COLORAMA_AVAILABLE = False
15
16class ColoredFormatter(logging.Formatter):
17 """
18 自定义格式化器,根据日志级别为日志消息添加颜色。
19 INFO: 绿色
20 WARNING: 黄色
21 ERROR: 红色
22 """
23 # 定义颜色映射
24 COLOR_MAP = {
25 logging.DEBUG: Fore.CYAN if COLORAMA_AVAILABLE else '',
26 logging.INFO: Fore.GREEN if COLORAMA_AVAILABLE else '',
27 logging.WARNING: Fore.YELLOW if COLORAMA_AVAILABLE else '',
28 logging.ERROR: Fore.RED if COLORAMA_AVAILABLE else '',
29 logging.CRITICAL: Fore.MAGENTA if COLORAMA_AVAILABLE else '',
30 }
31
32 RESET = Style.RESET_ALL if COLORAMA_AVAILABLE else ''
33
34 def format(self, record):
35 color = self.COLOR_MAP.get(record.levelno, '')
36 message = super().format(record)
37 if color:
38 message = f"{color}{message}{self.RESET}"
39 return message
40
41def setup_logger(logger_name, level=logging.INFO):
42 '''

Callers 1

setup_loggerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected