From 5a5b1057694778b9204fdb73467ad77ee3232313 Mon Sep 17 00:00:00 2001 From: Wenjie Zhang Date: Mon, 12 Jan 2026 19:22:41 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=9B=B4=E6=96=B0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=EF=BC=8C=E6=B7=BB=E5=8A=A0=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E6=A1=A5=E6=8E=A5=E5=8A=9F=E8=83=BD=20#462?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/knowledge/implementations/lightrag.py | 11 ----- src/utils/logging_config.py | 60 +++++++++++++++++++---- uv.lock | 2 +- 3 files changed, 52 insertions(+), 21 deletions(-) diff --git a/src/knowledge/implementations/lightrag.py b/src/knowledge/implementations/lightrag.py index e0cf1216..b1675cbf 100644 --- a/src/knowledge/implementations/lightrag.py +++ b/src/knowledge/implementations/lightrag.py @@ -32,17 +32,6 @@ class LightRagKB(KnowledgeBase): # 存储 LightRAG 实例映射 {db_id: LightRAG} self.instances: dict[str, LightRAG] = {} - # 设置 LightRAG 日志 - log_dir = os.path.join(work_dir, "logs", "lightrag") - os.makedirs(log_dir, exist_ok=True) - # 禁止调用 lightrag.utils.setup_logger,因为它会重置全局 logger 配置(调用 logger.remove()) - # 导致主程序的控制台输出丢失。 - # 全局 logger 已在 src/utils/logging_config.py 中配置好。 - # setup_logger( - # "lightrag", - # log_file_path=os.path.join(log_dir, f"lightrag_{shanghai_now().strftime('%Y-%m-%d')}.log"), - # ) - logger.info("LightRagKB initialized") @property diff --git a/src/utils/logging_config.py b/src/utils/logging_config.py index 8c3728b6..46342c97 100644 --- a/src/utils/logging_config.py +++ b/src/utils/logging_config.py @@ -1,3 +1,4 @@ +import logging import os import sys @@ -10,9 +11,50 @@ DATETIME = shanghai_now().strftime("%Y-%m-%d") LOG_FILE = f"{SAVE_DIR}/logs/yuxi-{DATETIME}.log" +class LoguruHandler(logging.Handler): + """将 Python logging 桥接到 loguru 的 handler""" + + def emit(self, record: logging.LogRecord): + level_map = { + logging.DEBUG: "DEBUG", + logging.INFO: "INFO", + logging.WARNING: "WARNING", + logging.ERROR: "ERROR", + logging.CRITICAL: "CRITICAL", + } + level = level_map.get(record.levelno, "DEBUG") + try: + msg = self.format(record) + except Exception: + msg = record.getMessage() + loguru_logger.opt(depth=1, exception=record.exc_info).log(level, msg) + + +def _setup_logging_bridge(): + """配置 logging 到 loguru 的桥接,捕获第三方库日志(如 LightRAG)""" + loguru_handler = LoguruHandler() + loguru_handler.setLevel(logging.DEBUG) + + formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") + loguru_handler.setFormatter(formatter) + + # 桥接 LightRAG 日志 + lightrag_logger = logging.getLogger("lightrag") + lightrag_logger.addHandler(loguru_handler) + lightrag_logger.setLevel(logging.DEBUG) + lightrag_logger.propagate = False # 避免重复 + + # 桥接其他常见第三方库(降低级别减少噪音) + for lib in ["httpx", "openai", "neo4j", "urllib3"]: + lib_logger = logging.getLogger(lib) + lib_logger.addHandler(loguru_handler) + lib_logger.setLevel(logging.WARNING) + lib_logger.propagate = False + + def setup_logger(name, level="DEBUG", console=True): """使用 loguru 设置日志记录器""" - os.makedirs(f"{SAVE_DIR}/logs", exist_ok=True) # 创建日志目录 + os.makedirs(f"{SAVE_DIR}/logs", exist_ok=True) # 移除默认的 handler loguru_logger.remove() @@ -23,10 +65,10 @@ def setup_logger(name, level="DEBUG", console=True): level=level, format="{time:YYYY-MM-DD HH:mm:ss} - {level} - {file}:{line} - {message}", encoding="utf-8", - rotation="10 MB", # 文件大小达到 10MB 时轮转 - retention="30 days", # 保留30天的日志 - compression="zip", # 压缩旧日志文件 - enqueue=True, # 异步写入,确保线程安全 + rotation="10 MB", + retention="30 days", + compression="zip", + enqueue=True, ) # 添加控制台日志(有颜色) @@ -41,7 +83,7 @@ def setup_logger(name, level="DEBUG", console=True): "{message}" ), colorize=True, - enqueue=True, # 异步写入,确保线程安全 + enqueue=True, ) return loguru_logger @@ -50,7 +92,7 @@ def setup_logger(name, level="DEBUG", console=True): # 设置根日志记录器 logger = setup_logger("Yuxi") -__all__ = ["logger"] +# 初始化 logging 桥接 +_setup_logging_bridge() -# If you want to disable logging from external libraries -# logging.getLogger('some_external_library').setLevel(logging.CRITICAL) +__all__ = ["logger"] diff --git a/uv.lock b/uv.lock index 6d0abbfc..3fb4e6c5 100644 --- a/uv.lock +++ b/uv.lock @@ -7336,7 +7336,7 @@ wheels = [ [[package]] name = "yuxi-know" -version = "0.4.0.dev0" +version = "0.4.2.dev0" source = { virtual = "." } dependencies = [ { name = "aiofiles" },