195 lines
5.5 KiB
Python
195 lines
5.5 KiB
Python
from loguru import logger
|
||
from typing import Dict, Optional, Union, List
|
||
import sys
|
||
from types import ModuleType
|
||
from pathlib import Path
|
||
|
||
# 保存原生处理器ID
|
||
default_handler_id = None
|
||
for handler_id in logger._core.handlers:
|
||
default_handler_id = handler_id
|
||
break
|
||
|
||
# 移除默认处理器
|
||
if default_handler_id is not None:
|
||
logger.remove(default_handler_id)
|
||
|
||
# 类型别名
|
||
LoguruLogger = logger.__class__
|
||
|
||
# 全局注册表:记录模块与处理器ID的映射
|
||
_handler_registry: Dict[str, List[int]] = {}
|
||
|
||
# 获取日志存储根地址
|
||
current_file_path = Path(__file__).resolve()
|
||
LOG_ROOT = "logs"
|
||
|
||
# 默认全局配置
|
||
DEFAULT_CONFIG = {
|
||
# 日志级别配置
|
||
"console_level": "DEBUG",
|
||
"file_level": "DEBUG",
|
||
|
||
# 格式配置
|
||
"console_format": (
|
||
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||
"<level>{level: <8}</level> | "
|
||
"<cyan>{extra[module]: <12}</cyan> | "
|
||
"<level>{message}</level>"
|
||
),
|
||
"file_format": (
|
||
"{time:YYYY-MM-DD HH:mm:ss} | "
|
||
"{level: <8} | "
|
||
"{extra[module]: <15} | "
|
||
"{message}"
|
||
),
|
||
"log_dir": LOG_ROOT,
|
||
"rotation": "00:00",
|
||
"retention": "3 days",
|
||
"compression": "zip",
|
||
}
|
||
|
||
|
||
def is_registered_module(record: dict) -> bool:
|
||
"""检查是否为已注册的模块"""
|
||
return record["extra"].get("module") in _handler_registry
|
||
|
||
|
||
def is_unregistered_module(record: dict) -> bool:
|
||
"""检查是否为未注册的模块"""
|
||
return not is_registered_module(record)
|
||
|
||
|
||
def log_patcher(record: dict) -> None:
|
||
"""自动填充未设置模块名的日志记录,保留原生模块名称"""
|
||
if "module" not in record["extra"]:
|
||
# 尝试从name中提取模块名
|
||
module_name = record.get("name", "")
|
||
if module_name == "":
|
||
module_name = "root"
|
||
record["extra"]["module"] = module_name
|
||
|
||
|
||
# 应用全局修补器
|
||
logger.configure(patcher=log_patcher)
|
||
|
||
|
||
class LogConfig:
|
||
"""日志配置类"""
|
||
|
||
def __init__(self, **kwargs):
|
||
self.config = DEFAULT_CONFIG.copy()
|
||
self.config.update(kwargs)
|
||
|
||
def to_dict(self) -> dict:
|
||
return self.config.copy()
|
||
|
||
def update(self, **kwargs):
|
||
self.config.update(kwargs)
|
||
|
||
|
||
def get_module_logger(
|
||
module: Union[str, ModuleType],
|
||
*,
|
||
console_level: Optional[str] = None,
|
||
file_level: Optional[str] = None,
|
||
extra_handlers: Optional[List[dict]] = None,
|
||
config: Optional[LogConfig] = None
|
||
) -> LoguruLogger:
|
||
module_name = module if isinstance(module, str) else module.__name__
|
||
current_config = config.config if config else DEFAULT_CONFIG
|
||
|
||
# 清理旧处理器
|
||
if module_name in _handler_registry:
|
||
for handler_id in _handler_registry[module_name]:
|
||
logger.remove(handler_id)
|
||
del _handler_registry[module_name]
|
||
|
||
handler_ids = []
|
||
|
||
# 控制台处理器
|
||
console_id = logger.add(
|
||
sink=sys.stderr,
|
||
level=console_level or current_config["console_level"],
|
||
format=current_config["console_format"],
|
||
filter=lambda record: record["extra"].get("module") == module_name,
|
||
enqueue=True,
|
||
)
|
||
handler_ids.append(console_id)
|
||
|
||
# 文件处理器
|
||
log_dir = Path(current_config["log_dir"])
|
||
log_dir.mkdir(parents=True, exist_ok=True)
|
||
log_file = log_dir / module_name / f"{{time:YYYY-MM-DD}}.log"
|
||
log_file.parent.mkdir(parents=True, exist_ok=True)
|
||
|
||
file_id = logger.add(
|
||
sink=str(log_file),
|
||
level=file_level or current_config["file_level"],
|
||
format=current_config["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,
|
||
enqueue=True,
|
||
)
|
||
handler_ids.append(file_id)
|
||
|
||
# 额外处理器
|
||
if extra_handlers:
|
||
for handler in extra_handlers:
|
||
handler_id = logger.add(**handler)
|
||
handler_ids.append(handler_id)
|
||
|
||
# 更新注册表
|
||
_handler_registry[module_name] = handler_ids
|
||
|
||
return logger.bind(module=module_name)
|
||
|
||
|
||
def remove_module_logger(module_name: str) -> None:
|
||
"""清理指定模块的日志处理器"""
|
||
if module_name in _handler_registry:
|
||
for handler_id in _handler_registry[module_name]:
|
||
logger.remove(handler_id)
|
||
del _handler_registry[module_name]
|
||
|
||
|
||
# 添加全局默认处理器(只处理未注册模块的日志--->控制台)
|
||
DEFAULT_GLOBAL_HANDLER = logger.add(
|
||
sink=sys.stderr,
|
||
level="DEBUG",
|
||
format=(
|
||
"<green>{time:YYYY-MM-DD HH:mm:ss}</green> | "
|
||
"<level>{level: <8}</level> | "
|
||
"<cyan>{name: <12}</cyan> | "
|
||
"<level>{message}</level>"
|
||
),
|
||
filter=is_unregistered_module, # 只处理未注册模块的日志
|
||
enqueue=True,
|
||
)
|
||
|
||
# 添加全局默认文件处理器(只处理未注册模块的日志--->logs文件夹)
|
||
log_dir = Path(DEFAULT_CONFIG["log_dir"])
|
||
log_dir.mkdir(parents=True, exist_ok=True)
|
||
other_log_dir = log_dir / "other"
|
||
other_log_dir.mkdir(parents=True, exist_ok=True)
|
||
|
||
DEFAULT_FILE_HANDLER = logger.add(
|
||
sink=str(other_log_dir / f"{{time:YYYY-MM-DD}}.log"),
|
||
level="DEBUG",
|
||
format=(
|
||
"{time:YYYY-MM-DD HH:mm:ss} | "
|
||
"{level: <8} | "
|
||
"{name: <15} | "
|
||
"{message}"
|
||
),
|
||
rotation=DEFAULT_CONFIG["rotation"],
|
||
retention=DEFAULT_CONFIG["retention"],
|
||
compression=DEFAULT_CONFIG["compression"],
|
||
encoding="utf-8",
|
||
filter=is_unregistered_module, # 只处理未注册模块的日志
|
||
enqueue=True,
|
||
)
|