fix(system): 强制以 UTF-8 编码读取系统日志文件,忽略编码错误

This commit is contained in:
付申祺 2026-05-14 13:04:17 +08:00
parent ef963617fc
commit c6c6527029

View File

@ -65,7 +65,8 @@ async def get_system_logs(levels: str | None = None, current_user: User = Depend
if levels:
level_filter = set(level.strip().upper() for level in levels.split(",") if level.strip())
async with aiofiles.open(LOG_FILE) as f:
# 修复 GBK 编码报错:强制 utf-8 读取,忽略错误
async with aiofiles.open(LOG_FILE, mode='r', encoding='utf-8', errors='ignore') as f:
# 读取最后1000行
lines = []
async for line in f:
@ -91,7 +92,6 @@ async def get_system_logs(levels: str | None = None, current_user: User = Depend
logger.error(f"获取系统日志失败: {e}")
raise HTTPException(status_code=500, detail=f"获取系统日志失败: {str(e)}")
# =============================================================================
# === 信息管理分组 ===
# =============================================================================