feat(audit): 平滑升级-监听器+装饰器共存,装饰器自动检测并跳过已处理日志

This commit is contained in:
DXC
2026-04-20 13:15:25 +08:00
parent becd3cb010
commit 381d1fa675
2 changed files with 387 additions and 0 deletions

View File

@ -301,6 +301,22 @@ def audit_log(module: str, action: str = None, get_target_id_fn=None, get_target
# 默认:记录请求 Payload
details = {'payload': filtered_payload}
# ★ 关键:检查是否已被底层监听器处理
# 如果底层已生成高级日志changes/deleted_snapshot则跳过简单的 payload 日志
from flask import g, has_request_context
if has_request_context() and hasattr(g, 'audit_handled_ids') and g.audit_handled_ids:
# 构建检查 key: "module:target_id"
check_key = f"{module}:{target_id}" if target_id else None
if check_key and check_key in g.audit_handled_ids:
# 底层已处理,跳过装饰器日志
current_app.logger.info(f'[审计] 底层监听器已处理 {check_key},跳过装饰器')
return response
# 也检查 target_name 是否匹配
for key in g.audit_handled_ids:
if target_name and target_name in key:
current_app.logger.info(f'[审计] 底层监听器已处理 {key},跳过装饰器')
return response
# 保存日志
log_entry = AuditLog(
user_id=user_id,