From ba31f5adc78b9013dbbde606986dac2a2a56e901 Mon Sep 17 00:00:00 2001 From: jingrow Date: Thu, 30 Oct 2025 21:06:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BE=8E=E5=8C=96=E8=B0=83=E8=AF=95=E6=97=A5?= =?UTF-8?q?=E5=BF=97=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/jingrow/jingrow/__init__.py | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/apps/jingrow/jingrow/__init__.py b/apps/jingrow/jingrow/__init__.py index 674f6bb..73f1e3e 100644 --- a/apps/jingrow/jingrow/__init__.py +++ b/apps/jingrow/jingrow/__init__.py @@ -15,13 +15,24 @@ _local = {} # 统一 Jingrow 日志记录器(仅为本模块及调用方提供最小可用输出,不修改全局 root logger) _root_logger = logging.getLogger("jingrow") +# 简单高效:用 addLevelName 给级别上色;使用标准 Formatter 调整顺序为“时间在最后” +_ANSI = { + 'DEBUG': '\x1b[36m', # cyan + 'INFO': '\x1b[32m', # green + 'WARNING': '\x1b[33m', # yellow + 'ERROR': '\x1b[31m', # red + 'CRITICAL': '\x1b[41m' # red background +} +for _lvl in list(_ANSI.keys()): + _code = _ANSI[_lvl] + logging.addLevelName(getattr(logging, _lvl), f"{_code}{_lvl}\x1b[0m") + def _ensure_logging_configured() -> None: if not _root_logger.handlers: handler = logging.StreamHandler() - formatter = logging.Formatter( - fmt="%(asctime)s %(levelname)s %(name)s - %(message)s" - ) + # 模块名 - 级别(已上色) - 消息 - 时间 + formatter = logging.Formatter("%(name)s - %(levelname)s - %(message)s - %(asctime)s") handler.setFormatter(formatter) _root_logger.addHandler(handler) # 从配置文件(Config)读取日志级别(默认 INFO)