Update Report Engine Log Filtering Strategy

This commit is contained in:
马一丁
2025-12-04 15:29:00 +08:00
parent ec1baf539c
commit 25abc25c9d
2 changed files with 56 additions and 1 deletions

View File

@@ -245,6 +245,31 @@ class ReportAgent:
log_dir = os.path.dirname(self.config.LOG_FILE)
os.makedirs(log_dir, exist_ok=True)
def _exclude_other_engines(record):
"""
过滤掉其他引擎(Insight/Media/Query/Forum)产生的日志,其余日志全部保留。
使用路径匹配为主,无法获取路径时退化到模块名。
"""
excluded_keywords = ("InsightEngine", "MediaEngine", "QueryEngine", "ForumEngine")
try:
file_path = record["file"].path
if any(keyword in file_path for keyword in excluded_keywords):
return False
except Exception:
pass
try:
module_name = record.get("module", "")
if isinstance(module_name, str):
lowered = module_name.lower()
if any(keyword.lower() in lowered for keyword in excluded_keywords):
return False
except Exception:
pass
return True
# 【修复】检查是否已经添加过这个文件的handler避免重复
# loguru会自动去重但显式检查更安全
log_file_path = str(Path(self.config.LOG_FILE).resolve())
@@ -274,7 +299,8 @@ class ReportAgent:
buffering=1, # 行缓冲,每行立即写入
serialize=False, # 普通文本格式不序列化为JSON
encoding="utf-8", # 明确UTF-8编码
mode="a" # 追加模式
mode="a", # 追加模式
filter=_exclude_other_engines # 过滤掉四个 Engine 的日志,保留其余信息
)
logger.debug(f"已添加日志handler (ID: {handler_id}): {self.config.LOG_FILE}")

View File

@@ -42,6 +42,33 @@ tasks_registry: Dict[str, 'ReportTask'] = {}
LOG_STREAM_LEVELS = {"DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"}
log_stream_handler_id: Optional[int] = None
EXCLUDED_ENGINE_PATH_KEYWORDS = ("ForumEngine", "InsightEngine", "MediaEngine", "QueryEngine")
def _is_excluded_engine_log(record: Dict[str, Any]) -> bool:
"""
判断日志是否来自其他引擎Insight/Media/Query/Forum用于过滤混入的日志。
返回:
bool: True 表示应当过滤(即不写入/不转发)。
"""
try:
file_path = record["file"].path
if any(keyword in file_path for keyword in EXCLUDED_ENGINE_PATH_KEYWORDS):
return True
except Exception:
pass
# 兜底尝试按模块名过滤防止file信息缺失时误混入
try:
module_name = record.get("module", "")
if isinstance(module_name, str):
lowered = module_name.lower()
return any(keyword.lower() in lowered for keyword in EXCLUDED_ENGINE_PATH_KEYWORDS)
except Exception:
pass
return False
def _stream_log_to_task(message):
"""
@@ -54,6 +81,8 @@ def _stream_log_to_task(message):
level_name = record["level"].name
if level_name not in LOG_STREAM_LEVELS:
return
if _is_excluded_engine_log(record):
return
with task_lock:
task = current_task