ApiLogAspect-日志-合并end

This commit is contained in:
khalil
2025-05-12 17:13:25 +08:00
parent ab60cbcb1d
commit c3b7b2cae9

View File

@@ -55,40 +55,37 @@ public class ApiRequestLogAspect {
if (SystemConfig.ApiLogConfig.excludes.contains(requestURI)) {
return joinPoint.proceed();
}
MDC.put(TRACE_UUID, UUID.randomUUID().toString());
requestInfo(joinPoint, request, requestURI);
Object result = null;
long start = System.currentTimeMillis();
try {
MDC.put(TRACE_UUID, UUID.randomUUID().toString());
log.info("====== Api called start ======");
Object result = joinPoint.proceed();
requestInfo(joinPoint, request, requestURI);
try {
if (result != null) {
if (SystemConfig.ApiLogConfig.notOutputResultUrls.contains(requestURI)) {
log.info("{} not print", requestURI);
} else {
log.info("{} return : {}", requestURI, objectMapper.writeValueAsString(result));
}
} else {
log.info("{} no result", requestURI);
}
} catch (Exception ignored) {
}
result = joinPoint.proceed();
return result;
} catch (Throwable e) {
requestInfo(joinPoint, request, requestURI);
log.error(requestURI + " is error ", e);
log.error("[URL] {} is error ", requestURI, e);
throw e;
} finally {
long end = System.currentTimeMillis();
log.info("====== Api called end ====== cost time : {} ms!", (end - start));
String resultStr = null == result? "no result":
SystemConfig.ApiLogConfig.notOutputResultUrls.contains(requestURI)? "no print":
String.format("return : %s", objectMapper.writeValueAsString(result));
log.info("====== Api called end ====== [URL] {} [CostTime] {} ms! [RESULT] {}", requestURI, (end - start), resultStr);
MDC.remove(TRACE_UUID);
}
}
private void requestInfo(ProceedingJoinPoint joinPoint, HttpServletRequest request, String uri) {
StringBuilder msg = new StringBuilder()
.append("====== Api called ======").append(" ")
.append("====== Api called start ======").append(" ")
.append("[URL]").append(uri).append(" ")
.append("[METHOD]").append(request.getMethod()).append(" ")
.append("[IP]").append(request.getRemoteAddr()).append(" ")