更新日志的中文说明

This commit is contained in:
wangjiming
2026-08-21 09:42:03 +08:00
parent 8455d99d49
commit 29d8a506cd
12 changed files with 1923 additions and 168 deletions

View File

@@ -457,9 +457,9 @@ def setup_request_logging(app: FastAPI) -> None:
elif response.status_code >= 400:
log_method = logger.warning
# 结构化访问日志
# 结构化访问日志(中文 message方便直接阅读
log_method(
"request completed",
f"HTTP请求 {request.method} {request.url.path}{response.status_code}(耗时{round(elapsed_ms, 2)}ms",
extra={
"request_method": request.method,
"request_path": request.url.path,
@@ -497,7 +497,7 @@ def setup_request_logging(app: FastAPI) -> None:
except Exception:
elapsed_ms = (time.perf_counter() - started_at) * 1000
logger.error(
"request failed",
f"HTTP请求异常 {request.method} {request.url.path}(耗时{round(elapsed_ms, 2)}ms— 服务内部错误",
extra={
"request_method": request.method,
"request_path": request.url.path,

View File

@@ -77,6 +77,92 @@ class OpStatus:
FAILURE = "failure"
# ==================== 中文映射表(让日志 message 直接可读)====================
MODULE_CN: dict[str, str] = {
"fine-tune": "模型训练",
"model-eval": "模型评测",
"model-inference": "模型推理",
"model-manage": "模型管理",
"dataset": "数据集",
"data-process": "数据处理",
"data-convert": "数据转换",
"compute": "算力节点",
"system": "系统",
}
ACTION_CN: dict[str, str] = {
"create": "创建",
"update": "更新",
"delete": "删除",
"start": "启动",
"stop": "停止",
"upload": "上传",
"download": "下载",
"convert": "转换",
"merge": "合并",
"import": "导入",
"login": "登录",
"logout": "退出登录",
"publish": "发布",
"retry": "重试",
"request": "请求",
}
TARGET_TYPE_CN: dict[str, str] = {
"fine_tune": "训练任务",
"eval": "评测任务",
"inference": "推理任务",
"model": "模型",
"trained_model": "训练产出模型",
"dataset": "数据集",
"dataset_version": "数据集版本",
"data_process_task": "数据处理任务",
"user": "用户",
"api": "接口",
}
def _build_cn_message(
module: str,
action: str,
target_type: str,
target_name: str | None,
target_id: str | None,
status: str,
username: str | None,
error_type: str,
error_message: str,
) -> str:
"""构建中文人类可读的日志消息,格式:[用户] 对 [模块] 执行了 [动作],结果:成功/失败"""
user_part = f"用户[{username}]" if username else "系统"
module_cn = MODULE_CN.get(module, module)
action_cn = ACTION_CN.get(action, action)
target_cn = TARGET_TYPE_CN.get(target_type, target_type or "")
target_label = target_name or target_id or ""
# 拼接操作对象描述
if target_cn and target_label:
target_part = f"{target_cn}{target_label}"
elif target_cn:
target_part = target_cn
elif target_label:
target_part = f"{target_label}"
else:
target_part = ""
if status == OpStatus.SUCCESS:
result = "成功"
msg = f"{user_part} {action_cn}{module_cn}{target_part},结果:成功"
else:
result = "失败"
err_brief = error_message[:120] if error_message else ""
err_part = f"{error_type}: {err_brief}" if error_type and err_brief else f"{error_type}" if error_type else ""
msg = f"{user_part} {action_cn}{module_cn}{target_part},结果:失败{err_part}"
return msg
def op_log(
module: str,
action: str,
@@ -354,6 +440,12 @@ def _write_log(
req_path = request.url.path
# ---- 写文件日志app-biz----
cn_msg = _build_cn_message(
module=module, action=action, target_type=target_type,
target_name=target_name, target_id=target_id, status=status,
username=username, error_type=error_type, error_message=error_message,
)
log_fields = {
"bizModule": module,
"action": action,
@@ -364,6 +456,10 @@ def _write_log(
"durationMs": round(duration_ms, 2),
"username": username or "",
}
if req_method:
log_fields["requestMethod"] = req_method
if req_path:
log_fields["requestPath"] = req_path
if detail:
log_fields["detail"] = detail[:500]
if error_message:
@@ -372,9 +468,9 @@ def _write_log(
log_fields["errorType"] = error_type
if status == OpStatus.SUCCESS:
biz_logger.info(f"{module}:{action} {status} - {target_name or target_id or ''}", **log_fields)
biz_logger.info(cn_msg, **log_fields)
elif status == OpStatus.FAILURE:
biz_logger.error(f"{module}:{action} {status} - {target_name or target_id or ''} - {error_type}: {error_message[:200]}", **log_fields)
biz_logger.error(cn_msg, **log_fields)
# ---- 写数据库 ----
try: