fix(logging): 优化API日志记录逻辑并改进异常处理
- 重构响应结果解析逻辑,支持多种状态字段判断 - 添加对空响应和异常响应的更完善处理 - 改进异常捕获和日志记录机制 - 将MDM服务中的异常抛出改为返回错误状态对象 - 修复finally块中日志保存可能发生的异常影响主业务流程
This commit is contained in:
@@ -54,7 +54,8 @@ public class ApiLogAspect {
|
||||
logEntity.setBizDesc(apiLog.bizDesc());
|
||||
logEntity.setTraceId(MDC.get("traceId"));
|
||||
logEntity.setCreateTime(DateUtil.now());
|
||||
|
||||
Object result = null;
|
||||
Throwable businessException = null;
|
||||
try {
|
||||
if (attributes != null) {
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
@@ -63,50 +64,77 @@ public class ApiLogAspect {
|
||||
logEntity.setRequestIp(getIpAddress(request));
|
||||
logEntity.setRequestHeaders(JSONUtil.toJsonStr(getRequestHeaders(request)));
|
||||
}
|
||||
|
||||
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
||||
Method method = signature.getMethod();
|
||||
Object[] args = joinPoint.getArgs();
|
||||
logEntity.setRequestParams(buildRequestParams(method, args));
|
||||
|
||||
Object result = joinPoint.proceed();
|
||||
|
||||
|
||||
JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(result));
|
||||
JSONObject jsonObject1 = jsonObject.getJSONObject("body");
|
||||
if(null == jsonObject1){
|
||||
logEntity.setResponseStatus(400);
|
||||
logEntity.setStatus("FAIL");
|
||||
logEntity.setErrorMsg("返回信息为空,请确认!");
|
||||
logEntity.setResponseBody("返回信息为空,请确认!");
|
||||
}else{
|
||||
String rtype = jsonObject1.getString("RTYPE");
|
||||
if(rtype.equalsIgnoreCase("E")){
|
||||
logEntity.setResponseStatus(400);
|
||||
logEntity.setStatus("FAIL");
|
||||
logEntity.setErrorMsg(JSONUtil.toJsonStr(result));
|
||||
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
|
||||
result = joinPoint.proceed();
|
||||
try {
|
||||
JSONObject jsonObject = JSONObject.parseObject(JSONUtil.toJsonStr(result));
|
||||
if(null != jsonObject && jsonObject.containsKey("status")){
|
||||
String status = jsonObject.getString("status");
|
||||
if ("E".equalsIgnoreCase(status)) {
|
||||
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));
|
||||
}
|
||||
}else{
|
||||
logEntity.setResponseStatus(200);
|
||||
logEntity.setStatus("SUCCESS");
|
||||
logEntity.setResponseBody(JSONUtil.toJsonStr(result));
|
||||
JSONObject jsonObject1 = jsonObject != null ? jsonObject.getJSONObject("body") : null;
|
||||
if (jsonObject1 == null) {
|
||||
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) {
|
||||
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;
|
||||
logEntity.setCostTime(costTime);
|
||||
logEntity.setStatus("FAIL");
|
||||
logEntity.setErrorMsg(throwable.getMessage());
|
||||
|
||||
throw throwable;
|
||||
|
||||
} finally {
|
||||
apiLogService.saveAsync(logEntity);
|
||||
try {
|
||||
apiLogService.saveAsync(logEntity);
|
||||
} catch (Exception e) {
|
||||
log.error("保存API日志失败,但不影响主业务", e);
|
||||
}
|
||||
if (businessException != null) {
|
||||
throw businessException;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private String getIpAddress(HttpServletRequest request) {
|
||||
|
||||
@@ -250,7 +250,9 @@ public class MdmToLmsServiceImpl implements MdmToLmsService {
|
||||
if (ObjectUtil.isEmpty(material_jo)) {
|
||||
JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0);
|
||||
if (ObjectUtil.isEmpty(unit)) {
|
||||
throw new BadRequestException("MDM->未查询到相关计量单位,请进行维护!");
|
||||
commonResponseDto.setStatus("E");
|
||||
commonResponseDto.setMessage("MDM->未查询到相关计量单位,请进行维护!");
|
||||
return commonResponseDto;
|
||||
}
|
||||
material_jo = new JSONObject();
|
||||
material_jo.put("material_id", mdId);
|
||||
@@ -268,7 +270,9 @@ public class MdmToLmsServiceImpl implements MdmToLmsService {
|
||||
} else {
|
||||
JSONObject unit = WQLObject.getWQLObject("md_pb_measureunit").query("unit_code = '" + meins + "' and is_delete ='0'").uniqueResult(0);
|
||||
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("base_unit_id", unit.getString("measure_unit_id"));
|
||||
|
||||
Reference in New Issue
Block a user