refactor(logger): 替换print为logger并添加自定义日志样式支持

将配置文件加载中的print语句替换为logger.info,以统一日志输出。同时新增log_decorators.py文件,提供自定义日志样式的装饰器支持,并在logger.py中实现自定义样式处理器的添加和移除功能。
This commit is contained in:
墨梓柒
2025-04-23 22:27:44 +08:00
parent 5782d4425b
commit 7438c2dd76
3 changed files with 245 additions and 56 deletions

View File

@@ -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 = "<bg yellow><black> SPECIAL [{time:HH:mm:ss}] </black></bg yellow> | <level>{message}</level>"
# @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="<cyan>任务:</cyan> {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("这是一条完全独立的标准日志")

View File

@@ -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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<level>{time:YYYY-MM-DD HH:mm:ss}</level> | "
"<cyan>{extra[module]: <12}</cyan> | "
"<level>{message}</level>"
),
@@ -59,7 +59,7 @@ else:
"console_level": "INFO",
"file_level": "DEBUG",
# 格式配置
"console_format": "<green>{time:MM-DD HH:mm}</green> | <cyan>{extra[module]}</cyan> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <cyan>{extra[module]}</cyan> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>海马体</light-yellow> | "
"<level>{message}</level>"
@@ -82,7 +82,7 @@ MEMORY_STYLE_CONFIG = {
},
"simple": {
"console_format": (
"<green>{time:MM-DD HH:mm}</green> | <light-yellow>海马体</light-yellow> | <light-yellow>{message}</light-yellow>"
"<level>{time:MM-DD HH:mm}</level> | <light-yellow>海马体</light-yellow> | <light-yellow>{message}</light-yellow>"
),
"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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>PFC</light-yellow> | "
"<level>{message}</level>"
@@ -102,7 +102,7 @@ PFC_STYLE_CONFIG = {
},
"simple": {
"console_format": (
"<green>{time:MM-DD HH:mm}</green> | <light-green>PFC</light-green> | <light-green>{message}</light-green>"
"<level>{time:MM-DD HH:mm}</level> | <light-green>PFC</light-green> | <light-green>{message}</light-green>"
),
"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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-green>心情</light-green> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <magenta>心情</magenta> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <magenta>心情</magenta> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<magenta>工具使用</magenta> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <magenta>工具使用</magenta> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <magenta>工具使用</magenta> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-magenta>关系</light-magenta> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <light-magenta>关系</light-magenta> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <light-magenta>关系</light-magenta> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-cyan>配置</light-cyan> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <light-cyan>配置</light-cyan> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <light-cyan>配置</light-cyan> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>消息发送</light-yellow> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <green>消息发送</green> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <green>消息发送</green> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>麦麦大脑袋</light-yellow> | "
"<level>{message}</level>"
@@ -210,7 +210,7 @@ HEARTFLOW_STYLE_CONFIG = {
},
"simple": {
"console_format": (
"<green>{time:MM-DD HH:mm}</green> | <light-green>麦麦大脑袋</light-green> | <light-green>{message}</light-green>"
"<level>{time:MM-DD HH:mm}</level> | <light-green>麦麦大脑袋</light-green> | <light-green>{message}</light-green>"
), # 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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>在干嘛</light-yellow> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <cyan>在干嘛</cyan> | <cyan>{message}</cyan>",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <cyan>在干嘛</cyan> | <cyan>{message}</cyan>",
"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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>麦麦组织语言</light-yellow> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <light-green>麦麦组织语言</light-green> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <light-green>麦麦组织语言</light-green> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-blue>话题</light-blue> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <light-blue>主题</light-blue> | {message}",
"console_format": "<level>{time:MM-DD HH:mm}</level> | <light-blue>主题</light-blue> | {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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-blue>见闻</light-blue> | "
"<level>{message}</level>"
@@ -283,7 +283,7 @@ CHAT_STYLE_CONFIG = {
},
"simple": {
"console_format": (
"<green>{time:MM-DD HH:mm}</green> | <light-blue>见闻</light-blue> | <green>{message}</green>"
"<level>{time:MM-DD HH:mm}</level> | <light-blue>见闻</light-blue> | <green>{message}</green>"
), # 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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-blue>麦麦小脑袋</light-blue> | "
"<level>{message}</level>"
@@ -302,7 +302,7 @@ SUB_HEARTFLOW_STYLE_CONFIG = {
},
"simple": {
"console_format": (
"<green>{time:MM-DD HH:mm}</green> | <light-blue>麦麦小脑袋</light-blue> | <light-blue>{message}</light-blue>"
"<level>{time:MM-DD HH:mm}</level> | <light-blue>麦麦小脑袋</light-blue> | <light-blue>{message}</light-blue>"
), # 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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-blue>意愿</light-blue> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <light-blue>意愿 | {message} </light-blue>", # noqa: E501
"console_format": "<level>{time:MM-DD HH:mm}</level> | <light-blue>意愿 | {message} </light-blue>", # 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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-blue>麦麦状态</light-blue> | "
"<level>{message}</level>"
@@ -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": "<green>{time:MM-DD HH:mm}</green> | <light-blue>麦麦状态 | {message} </light-blue>", # noqa: E501
"console_format": "<level>{time:MM-DD HH:mm}</level> | <light-blue>麦麦状态 | {message} </light-blue>", # 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": (
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
"<level>{level: <8}</level> | "
"<white>{time:YYYY-MM-DD HH:mm:ss}</white> | "
"{level: <8} | "
"<cyan>{extra[module]: <12}</cyan> | "
"<light-yellow>LPMM</light-yellow> | "
"<level>{message}</level>"
@@ -357,7 +357,7 @@ LPMM_STYLE_CONFIG = {
},
"simple": {
"console_format": (
"<green>{time:MM-DD HH:mm}</green> | <light-green>LPMM</light-green> | <light-green>{message}</light-green>"
"<level>{time:MM-DD HH:mm}</level> | <light-green>LPMM</light-green> | <light-green>{message}</light-green>"
),
"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: