From 7438c2dd76a367a5a3ded5b2f462e3a05b10436f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=A2=A8=E6=A2=93=E6=9F=92?= <1787882683@qq.com>
Date: Wed, 23 Apr 2025 22:27:44 +0800
Subject: [PATCH] =?UTF-8?q?refactor(logger):=20=E6=9B=BF=E6=8D=A2print?=
=?UTF-8?q?=E4=B8=BAlogger=E5=B9=B6=E6=B7=BB=E5=8A=A0=E8=87=AA=E5=AE=9A?=
=?UTF-8?q?=E4=B9=89=E6=97=A5=E5=BF=97=E6=A0=B7=E5=BC=8F=E6=94=AF=E6=8C=81?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
将配置文件加载中的print语句替换为logger.info,以统一日志输出。同时新增log_decorators.py文件,提供自定义日志样式的装饰器支持,并在logger.py中实现自定义样式处理器的添加和移除功能。
---
src/common/log_decorators.py | 107 ++++++++++++++
src/common/logger.py | 189 +++++++++++++++++-------
src/plugins/knowledge/src/lpmmconfig.py | 5 +-
3 files changed, 245 insertions(+), 56 deletions(-)
create mode 100644 src/common/log_decorators.py
diff --git a/src/common/log_decorators.py b/src/common/log_decorators.py
new file mode 100644
index 000000000..60d9c6cc9
--- /dev/null
+++ b/src/common/log_decorators.py
@@ -0,0 +1,107 @@
+import functools
+import inspect
+from typing import Callable, Any
+from .logger import logger, add_custom_style_handler
+
+
+def use_log_style(
+ style_name: str,
+ console_format: str,
+ console_level: str = "INFO",
+ # file_format: Optional[str] = None, # 暂未支持文件输出
+ # file_level: str = "DEBUG",
+) -> Callable:
+ """装饰器:为函数内的日志启用特定的自定义样式。
+
+ Args:
+ style_name (str): 自定义样式的唯一名称。
+ console_format (str): 控制台输出的格式字符串。
+ console_level (str, optional): 控制台日志级别. Defaults to "INFO".
+ # file_format (Optional[str], optional): 文件输出格式 (暂未支持). Defaults to None.
+ # file_level (str, optional): 文件日志级别 (暂未支持). Defaults to "DEBUG".
+
+ Returns:
+ Callable: 返回装饰器本身。
+ """
+
+ def decorator(func: Callable) -> Callable:
+ # 获取被装饰函数所在的模块名
+ module = inspect.getmodule(func)
+ if module is None:
+ # 如果无法获取模块(例如,在交互式解释器中定义函数),则使用默认名称
+ module_name = "unknown_module"
+ logger.warning(f"无法确定函数 {func.__name__} 的模块,将使用 '{module_name}'")
+ else:
+ module_name = module.__name__
+
+ # 在函数首次被调用(或模块加载时)确保自定义处理器已添加
+ # 注意:这会在模块加载时执行,而不是每次函数调用时
+ # print(f"Setting up custom style '{style_name}' for module '{module_name}' in decorator definition")
+ add_custom_style_handler(
+ module_name=module_name,
+ style_name=style_name,
+ console_format=console_format,
+ console_level=console_level,
+ # file_format=file_format,
+ # file_level=file_level,
+ )
+
+ @functools.wraps(func)
+ def wrapper(*args: Any, **kwargs: Any) -> Any:
+ # 创建绑定了模块名和自定义样式标记的 logger 实例
+ custom_logger = logger.bind(module=module_name, custom_style=style_name)
+ # print(f"Executing {func.__name__} with custom logger for style '{style_name}'")
+ # 将自定义 logger 作为第一个参数传递给原函数
+ # 注意:这要求被装饰的函数第一个参数用于接收 logger
+ try:
+ return func(custom_logger, *args, **kwargs)
+ except TypeError as e:
+ # 捕获可能的类型错误,比如原函数不接受 logger 参数
+ logger.error(
+ f"调用 {func.__name__} 时出错:请确保该函数接受一个 logger 实例作为其第一个参数。错误:{e}"
+ )
+ # 可以选择重新抛出异常或返回特定值
+ raise e
+
+ return wrapper
+
+ return decorator
+
+
+# --- 示例用法 (可以在其他模块中这样使用) ---
+
+# # 假设这是你的模块 my_module.py
+# from src.common.log_decorators import use_log_style
+# from src.common.logger import get_module_logger, LoguruLogger
+
+# # 获取模块的标准 logger
+# standard_logger = get_module_logger(__name__)
+
+# # 定义一个自定义样式
+# MY_SPECIAL_STYLE = "special"
+# MY_SPECIAL_FORMAT = " SPECIAL [{time:HH:mm:ss}] | {message}"
+
+# @use_log_style(style_name=MY_SPECIAL_STYLE, console_format=MY_SPECIAL_FORMAT)
+# def my_function_with_special_logs(custom_logger: LoguruLogger, x: int, y: int):
+# standard_logger.info("这是一条使用标准格式的日志")
+# custom_logger.info(f"开始执行特殊操作,参数: x={x}, y={y}")
+# result = x + y
+# custom_logger.success(f"特殊操作完成,结果: {result}")
+# standard_logger.info("标准格式日志:函数即将结束")
+# return result
+
+# @use_log_style(style_name="another_style", console_format="任务: {message}")
+# def another_task(task_logger: LoguruLogger, task_name: str):
+# standard_logger.debug("准备执行另一个任务")
+# task_logger.info(f"正在处理任务 '{task_name}'")
+# # ... 执行任务 ...
+# task_logger.warning("任务处理中遇到一个警告")
+# standard_logger.info("另一个任务的标准日志")
+
+# if __name__ == "__main__":
+# print("\n--- 调用 my_function_with_special_logs ---")
+# my_function_with_special_logs(10, 5)
+# print("\n--- 调用 another_task ---")
+# another_task("数据清理")
+# print("\n--- 单独使用标准 logger ---")
+# standard_logger.info("这是一条完全独立的标准日志")
\ No newline at end of file
diff --git a/src/common/logger.py b/src/common/logger.py
index 2b2208419..cbc6c1499 100644
--- a/src/common/logger.py
+++ b/src/common/logger.py
@@ -1,5 +1,5 @@
from loguru import logger
-from typing import Dict, Optional, Union, List
+from typing import Dict, Optional, Union, List, Tuple
import sys
import os
from types import ModuleType
@@ -26,6 +26,7 @@ LoguruLogger = logger.__class__
# 全局注册表:记录模块与处理器ID的映射
_handler_registry: Dict[str, List[int]] = {}
+_custom_style_handlers: Dict[Tuple[str, str], List[int]] = {} # 记录自定义样式处理器ID
# 获取日志存储根地址
current_file_path = Path(__file__).resolve()
@@ -42,8 +43,7 @@ if not SIMPLE_OUTPUT:
"file_level": "DEBUG",
# 格式配置
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
"{extra[module]: <12} | "
"{message}"
),
@@ -59,7 +59,7 @@ else:
"console_level": "INFO",
"file_level": "DEBUG",
# 格式配置
- "console_format": "{time:MM-DD HH:mm} | {extra[module]} | {message}",
+ "console_format": "{time:MM-DD HH:mm} | {extra[module]} | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | {message}",
"log_dir": LOG_ROOT,
"rotation": "00:00",
@@ -72,8 +72,8 @@ else:
MEMORY_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"海马体 | "
"{message}"
@@ -82,7 +82,7 @@ MEMORY_STYLE_CONFIG = {
},
"simple": {
"console_format": (
- "{time:MM-DD HH:mm} | 海马体 | {message}"
+ "{time:MM-DD HH:mm} | 海马体 | {message}"
),
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 海马体 | {message}",
},
@@ -92,8 +92,8 @@ MEMORY_STYLE_CONFIG = {
PFC_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"PFC | "
"{message}"
@@ -102,7 +102,7 @@ PFC_STYLE_CONFIG = {
},
"simple": {
"console_format": (
- "{time:MM-DD HH:mm} | PFC | {message}"
+ "{time:MM-DD HH:mm} | PFC | {message}"
),
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | PFC | {message}",
},
@@ -112,8 +112,8 @@ PFC_STYLE_CONFIG = {
MOOD_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"心情 | "
"{message}"
@@ -121,7 +121,7 @@ MOOD_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 心情 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 心情 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 心情 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 心情 | {message}",
},
}
@@ -129,8 +129,8 @@ MOOD_STYLE_CONFIG = {
TOOL_USE_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"工具使用 | "
"{message}"
@@ -138,7 +138,7 @@ TOOL_USE_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 工具使用 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 工具使用 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 工具使用 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 工具使用 | {message}",
},
}
@@ -148,8 +148,8 @@ TOOL_USE_STYLE_CONFIG = {
RELATION_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"关系 | "
"{message}"
@@ -157,7 +157,7 @@ RELATION_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 关系 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 关系 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 关系 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 关系 | {message}",
},
}
@@ -166,8 +166,8 @@ RELATION_STYLE_CONFIG = {
CONFIG_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"配置 | "
"{message}"
@@ -175,7 +175,7 @@ CONFIG_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 配置 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 配置 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 配置 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 配置 | {message}",
},
}
@@ -183,8 +183,8 @@ CONFIG_STYLE_CONFIG = {
SENDER_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"消息发送 | "
"{message}"
@@ -192,7 +192,7 @@ SENDER_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 消息发送 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 消息发送 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 消息发送 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 消息发送 | {message}",
},
}
@@ -200,8 +200,8 @@ SENDER_STYLE_CONFIG = {
HEARTFLOW_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"麦麦大脑袋 | "
"{message}"
@@ -210,7 +210,7 @@ HEARTFLOW_STYLE_CONFIG = {
},
"simple": {
"console_format": (
- "{time:MM-DD HH:mm} | 麦麦大脑袋 | {message}"
+ "{time:MM-DD HH:mm} | 麦麦大脑袋 | {message}"
), # noqa: E501
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦大脑袋 | {message}",
},
@@ -219,8 +219,8 @@ HEARTFLOW_STYLE_CONFIG = {
SCHEDULE_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"在干嘛 | "
"{message}"
@@ -228,7 +228,7 @@ SCHEDULE_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 在干嘛 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 在干嘛 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 在干嘛 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 在干嘛 | {message}",
},
}
@@ -236,8 +236,8 @@ SCHEDULE_STYLE_CONFIG = {
LLM_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"麦麦组织语言 | "
"{message}"
@@ -245,7 +245,7 @@ LLM_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦组织语言 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 麦麦组织语言 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 麦麦组织语言 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦组织语言 | {message}",
},
}
@@ -255,8 +255,8 @@ LLM_STYLE_CONFIG = {
TOPIC_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"话题 | "
"{message}"
@@ -264,7 +264,7 @@ TOPIC_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 话题 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 主题 | {message}",
+ "console_format": "{time:MM-DD HH:mm} | 主题 | {message}",
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 话题 | {message}",
},
}
@@ -273,8 +273,8 @@ TOPIC_STYLE_CONFIG = {
CHAT_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"见闻 | "
"{message}"
@@ -283,7 +283,7 @@ CHAT_STYLE_CONFIG = {
},
"simple": {
"console_format": (
- "{time:MM-DD HH:mm} | 见闻 | {message}"
+ "{time:MM-DD HH:mm} | 见闻 | {message}"
), # noqa: E501
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 见闻 | {message}",
},
@@ -292,8 +292,8 @@ CHAT_STYLE_CONFIG = {
SUB_HEARTFLOW_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"麦麦小脑袋 | "
"{message}"
@@ -302,7 +302,7 @@ SUB_HEARTFLOW_STYLE_CONFIG = {
},
"simple": {
"console_format": (
- "{time:MM-DD HH:mm} | 麦麦小脑袋 | {message}"
+ "{time:MM-DD HH:mm} | 麦麦小脑袋 | {message}"
), # noqa: E501
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦小脑袋 | {message}",
},
@@ -311,8 +311,8 @@ SUB_HEARTFLOW_STYLE_CONFIG = {
WILLING_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"意愿 | "
"{message}"
@@ -320,7 +320,7 @@ WILLING_STYLE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 意愿 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 意愿 | {message} ", # noqa: E501
+ "console_format": "{time:MM-DD HH:mm} | 意愿 | {message} ", # noqa: E501
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 意愿 | {message}",
},
}
@@ -329,8 +329,8 @@ WILLING_STYLE_CONFIG = {
MAI_STATE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"麦麦状态 | "
"{message}"
@@ -338,7 +338,7 @@ MAI_STATE_CONFIG = {
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦状态 | {message}",
},
"simple": {
- "console_format": "{time:MM-DD HH:mm} | 麦麦状态 | {message} ", # noqa: E501
+ "console_format": "{time:MM-DD HH:mm} | 麦麦状态 | {message} ", # noqa: E501
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | 麦麦状态 | {message}",
},
}
@@ -347,8 +347,8 @@ MAI_STATE_CONFIG = {
LPMM_STYLE_CONFIG = {
"advanced": {
"console_format": (
- "{time:YYYY-MM-DD HH:mm:ss} | "
- "{level: <8} | "
+ "{time:YYYY-MM-DD HH:mm:ss} | "
+ "{level: <8} | "
"{extra[module]: <12} | "
"LPMM | "
"{message}"
@@ -357,7 +357,7 @@ LPMM_STYLE_CONFIG = {
},
"simple": {
"console_format": (
- "{time:MM-DD HH:mm} | LPMM | {message}"
+ "{time:MM-DD HH:mm} | LPMM | {message}"
),
"file_format": "{time:YYYY-MM-DD HH:mm:ss} | {level: <8} | {extra[module]: <15} | LPMM | {message}",
},
@@ -451,7 +451,7 @@ def get_module_logger(
sink=sys.stderr,
level=os.getenv("CONSOLE_LOG_LEVEL", console_level or current_config["console_level"]),
format=current_config["console_format"],
- filter=lambda record: record["extra"].get("module") == module_name,
+ filter=lambda record: record["extra"].get("module") == module_name and 'custom_style' not in record['extra'],
enqueue=True,
)
handler_ids.append(console_id)
@@ -470,7 +470,7 @@ def get_module_logger(
retention=current_config["retention"],
compression=current_config["compression"],
encoding="utf-8",
- filter=lambda record: record["extra"].get("module") == module_name,
+ filter=lambda record: record["extra"].get("module") == module_name and 'custom_style' not in record['extra'],
enqueue=True,
)
handler_ids.append(file_id)
@@ -487,6 +487,87 @@ def get_module_logger(
return logger.bind(module=module_name)
+def add_custom_style_handler(
+ module_name: str,
+ style_name: str,
+ console_format: str,
+ console_level: str = "INFO",
+ # file_format: Optional[str] = None, # 暂时只支持控制台
+ # file_level: str = "DEBUG",
+ # config: Optional[LogConfig] = None, # 暂时不使用全局配置
+) -> None:
+ """为指定模块和样式名添加自定义日志处理器(目前仅支持控制台)."""
+ handler_key = (module_name, style_name)
+
+ # 如果已存在该模块和样式的处理器,则不重复添加
+ if handler_key in _custom_style_handlers:
+ # print(f"Custom handler for {handler_key} already exists.")
+ return
+
+ handler_ids = []
+
+ # 添加自定义控制台处理器
+ try:
+ custom_console_id = logger.add(
+ sink=sys.stderr,
+ level=os.getenv(f"{module_name.upper()}_{style_name.upper()}_CONSOLE_LEVEL", console_level),
+ format=console_format,
+ filter=lambda record: record["extra"].get("module") == module_name
+ and record["extra"].get("custom_style") == style_name,
+ enqueue=True,
+ )
+ handler_ids.append(custom_console_id)
+ # print(f"Added custom console handler {custom_console_id} for {handler_key}")
+ except Exception as e:
+ logger.error(f"Failed to add custom console handler for {handler_key}: {e}")
+ # 如果添加失败,确保列表为空,避免记录不存在的ID
+ handler_ids = []
+
+ # # 文件处理器 (可选,按需启用)
+ # if file_format:
+ # current_config = config.config if config else DEFAULT_CONFIG
+ # log_dir = Path(current_config["log_dir"])
+ # log_dir.mkdir(parents=True, exist_ok=True)
+ # # 可以考虑将自定义样式的日志写入单独文件或模块主文件
+ # log_file = log_dir / module_name / f"{style_name}_{{time:YYYY-MM-DD}}.log"
+ # log_file.parent.mkdir(parents=True, exist_ok=True)
+ # try:
+ # custom_file_id = logger.add(
+ # sink=str(log_file),
+ # level=os.getenv(f"{module_name.upper()}_{style_name.upper()}_FILE_LEVEL", file_level),
+ # format=file_format,
+ # rotation=current_config["rotation"],
+ # retention=current_config["retention"],
+ # compression=current_config["compression"],
+ # encoding="utf-8",
+ # filter=lambda record: record["extra"].get("module") == module_name
+ # and record["extra"].get("custom_style") == style_name,
+ # enqueue=True,
+ # )
+ # handler_ids.append(custom_file_id)
+ # except Exception as e:
+ # logger.error(f"Failed to add custom file handler for {handler_key}: {e}")
+
+ # 更新自定义处理器注册表
+ if handler_ids:
+ _custom_style_handlers[handler_key] = handler_ids
+
+
+def remove_custom_style_handler(module_name: str, style_name: str) -> None:
+ """移除指定模块和样式名的自定义日志处理器."""
+ handler_key = (module_name, style_name)
+ if handler_key in _custom_style_handlers:
+ for handler_id in _custom_style_handlers[handler_key]:
+ try:
+ logger.remove(handler_id)
+ # print(f"Removed custom handler {handler_id} for {handler_key}")
+ except ValueError:
+ # 可能已经被移除或不存在
+ # print(f"Handler {handler_id} for {handler_key} already removed or invalid.")
+ pass
+ del _custom_style_handlers[handler_key]
+
+
def remove_module_logger(module_name: str) -> None:
"""清理指定模块的日志处理器"""
if module_name in _handler_registry:
diff --git a/src/plugins/knowledge/src/lpmmconfig.py b/src/plugins/knowledge/src/lpmmconfig.py
index 7f59bc897..753562f45 100644
--- a/src/plugins/knowledge/src/lpmmconfig.py
+++ b/src/plugins/knowledge/src/lpmmconfig.py
@@ -2,6 +2,7 @@ import os
import toml
import sys
import argparse
+from .global_logger import logger
PG_NAMESPACE = "paragraph"
ENT_NAMESPACE = "entity"
@@ -63,8 +64,8 @@ def _load_config(config, config_file_path):
if "persistence" in file_config:
config["persistence"] = file_config["persistence"]
- print(config)
- print("Configurations loaded from file: ", config_file_path)
+ # print(config)
+ logger.info(f"从文件中读取配置: {config_file_path}")
parser = argparse.ArgumentParser(description="Configurations for the pipeline")