fix(logging): 优化API日志记录逻辑并改进异常处理

- 重构响应结果解析逻辑,支持多种状态字段判断
- 添加对空响应和异常响应的更完善处理
- 改进异常捕获和日志记录机制
- 将MDM服务中的异常抛出改为返回错误状态对象
- 修复finally块中日志保存可能发生的异常影响主业务流程
This commit is contained in:
yangyufu
2026-06-16 11:16:40 +08:00
parent 3b1d413025
commit 0a6bcde1b6
2 changed files with 68 additions and 36 deletions

View File

@@ -54,7 +54,8 @@ public class ApiLogAspect {
logEntity.setBizDesc(apiLog.bizDesc()); logEntity.setBizDesc(apiLog.bizDesc());
logEntity.setTraceId(MDC.get("traceId")); logEntity.setTraceId(MDC.get("traceId"));
logEntity.setCreateTime(DateUtil.now()); logEntity.setCreateTime(DateUtil.now());
Object result = null;
Throwable businessException = null;
try { try {
if (attributes != null) { if (attributes != null) {
HttpServletRequest request = attributes.getRequest(); HttpServletRequest request = attributes.getRequest();
@@ -63,50 +64,77 @@ public class ApiLogAspect {
logEntity.setRequestIp(getIpAddress(request)); logEntity.setRequestIp(getIpAddress(request));
logEntity.setRequestHeaders(JSONUtil.toJsonStr(getRequestHeaders(request))); logEntity.setRequestHeaders(JSONUtil.toJsonStr(getRequestHeaders(request)));
} }
MethodSignature signature = (MethodSignature) joinPoint.getSignature(); MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod(); Method method = signature.getMethod();
Object[] args = joinPoint.getArgs(); Object[] args = joinPoint.getArgs();
logEntity.setRequestParams(buildRequestParams(method, args)); logEntity.setRequestParams(buildRequestParams(method, args));
result = joinPoint.proceed();
Object result = joinPoint.proceed(); try {
JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(result));
if(null != jsonObject && jsonObject.containsKey("status")){
JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(result)); String status = jsonObject.getString("status");
JSONObject jsonObject1 = jsonObject.getJSONObject("body"); if ("E".equalsIgnoreCase(status)) {
if(null == jsonObject1){ logEntity.setResponseStatus(400);
logEntity.setResponseStatus(400); logEntity.setStatus("FAIL");
logEntity.setStatus("FAIL"); logEntity.setErrorMsg(JSONUtil.toJsonStr(result));
logEntity.setErrorMsg("返回信息为空,请确认!"); logEntity.setResponseBody(JSONUtil.toJsonStr(result));
logEntity.setResponseBody("返回信息为空,请确认!"); } else {
}else{ logEntity.setResponseStatus(200);
String rtype = jsonObject1.getString("RTYPE"); logEntity.setStatus("SUCCESS");
if(rtype.equalsIgnoreCase("E")){ logEntity.setResponseBody(JSONUtil.toJsonStr(result));
logEntity.setResponseStatus(400); }
logEntity.setStatus("FAIL");
logEntity.setErrorMsg(JSONUtil.toJsonStr(result));
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
}else{ }else{
logEntity.setResponseStatus(200); JSONObject jsonObject1 = jsonObject != null ? jsonObject.getJSONObject("body") : null;
logEntity.setStatus("SUCCESS"); if (jsonObject1 == null) {
logEntity.setResponseBody(JSONUtil.toJsonStr(result)); logEntity.setResponseStatus(400);
logEntity.setStatus("FAIL");
logEntity.setErrorMsg("返回信息为空,请确认!");
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
} else {
String key = "";
if (jsonObject1.containsKey("RTYPE")) {
key = "RTYPE";
} else if (jsonObject1.containsKey("TYPE")) {
key = "TYPE";
}
String rtype = StrUtil.isNotBlank(key) ? jsonObject1.getString(key) : null;
if ("E".equalsIgnoreCase(rtype)) {
logEntity.setResponseStatus(400);
logEntity.setStatus("FAIL");
logEntity.setErrorMsg(JSONUtil.toJsonStr(result));
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
} else {
logEntity.setResponseStatus(200);
logEntity.setStatus("SUCCESS");
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
}
}
} }
} catch (Exception e) {
log.error("解析响应结果失败,记录原始响应", e);
logEntity.setResponseStatus(200);
logEntity.setStatus("SUCCESS");
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
} }
long costTime = System.currentTimeMillis() - startTime;
logEntity.setCostTime(costTime);
return result;
} catch (Throwable throwable) { } catch (Throwable throwable) {
businessException = throwable;
logEntity.setStatus("FAIL");
String errorMsg = throwable.getMessage();
logEntity.setErrorMsg(errorMsg != null && errorMsg.length() > 2000 ?
errorMsg.substring(0, 2000) : errorMsg);
} finally {
long costTime = System.currentTimeMillis() - startTime; long costTime = System.currentTimeMillis() - startTime;
logEntity.setCostTime(costTime); logEntity.setCostTime(costTime);
logEntity.setStatus("FAIL"); try {
logEntity.setErrorMsg(throwable.getMessage()); apiLogService.saveAsync(logEntity);
} catch (Exception e) {
throw throwable; log.error("保存API日志失败但不影响主业务", e);
}
} finally { if (businessException != null) {
apiLogService.saveAsync(logEntity); throw businessException;
}
} }
return result;
} }
private String getIpAddress(HttpServletRequest request) { private String getIpAddress(HttpServletRequest request) {

View File

@@ -250,7 +250,9 @@ public class MdmToLmsServiceImpl implements MdmToLmsService {
if (ObjectUtil.isEmpty(material_jo)) { if (ObjectUtil.isEmpty(material_jo)) {
JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0); JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0);
if (ObjectUtil.isEmpty(unit)) { if (ObjectUtil.isEmpty(unit)) {
throw new BadRequestException("MDM->未查询到相关计量单位,请进行维护!"); commonResponseDto.setStatus("E");
commonResponseDto.setMessage("MDM->未查询到相关计量单位,请进行维护!");
return commonResponseDto;
} }
material_jo = new JSONObject(); material_jo = new JSONObject();
material_jo.put("material_id", mdId); material_jo.put("material_id", mdId);
@@ -268,7 +270,9 @@ public class MdmToLmsServiceImpl implements MdmToLmsService {
} else { } else {
JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0); JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0);
if (ObjectUtil.isEmpty(unit)) { if (ObjectUtil.isEmpty(unit)) {
throw new BadRequestException("MDM->未查询到相关计量单位,请进行维护!"); commonResponseDto.setStatus("E");
commonResponseDto.setMessage("MDM->未查询到相关计量单位,请进行维护!");
return commonResponseDto;
} }
material_jo.put("material_name", mdDescription); material_jo.put("material_name", mdDescription);
material_jo.put("base_unit_id", unit.getString("measure_unit_id")); material_jo.put("base_unit_id", unit.getString("measure_unit_id"));