diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java index 7f9399c5e1..58c91650f3 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java @@ -1,10 +1,12 @@ package com.epmet.commons.tools.aspect; +import com.epmet.commons.tools.constant.AppClientConstant; import com.epmet.commons.tools.exception.ErrorCode; import com.epmet.commons.tools.exception.ExceptionUtils; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.exception.ValidateException; import com.epmet.commons.tools.utils.Result; +import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,6 +15,7 @@ import org.springframework.dao.DuplicateKeyException; import javax.servlet.http.HttpServletRequest; import java.time.Duration; import java.time.LocalDateTime; +import java.util.UUID; /** * 日志切面 @@ -47,56 +50,65 @@ public abstract class BaseRequestLogAspect { protected Object proceed(ProceedingJoinPoint point, HttpServletRequest request) throws Throwable { String requestURI = request.getRequestURI(); - long threadId = Thread.currentThread().getId(); + // 获取事务流水号 + String transactionSerial = request.getHeader(AppClientConstant.TRANSACTION_SERIAL_KEY); + //if (StringUtils.isEmpty(transactionSerial)) { + // transactionSerial = UUID.randomUUID().toString(); + //} + + // 将当前线程名称设置为事务流水号 + if (!StringUtils.isEmpty(transactionSerial)) { + Thread.currentThread().setName(transactionSerial); + } Object result; LocalDateTime startTime = LocalDateTime.now(); try { Object[] args = point.getArgs(); - log.info(">>>>>>>>请求信息>>>>>>>>:线程ID:{},url:{},请求参数:{}", threadId, requestURI, objectsToString(args)); + log.info(">>>>>>>>请求信息>>>>>>>>:事务流水号:{},url:{},请求参数:{}", transactionSerial, requestURI, objectsToString(args)); result = point.proceed(); - resultInfoLog(threadId, getExecPeriod(startTime), result); + resultInfoLog(transactionSerial, getExecPeriod(startTime), result); } catch (RenException e) { result = handleRenException(e); - resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); + resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); } catch (ValidateException e) { result = handleValidateException(e); - resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); + resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMsg(), ExceptionUtils.getErrorStackTrace(e)); } catch (DuplicateKeyException e) { result = handlerDuplicateKeyException(e); - resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); + resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); } catch (RuntimeException re) { result = handlerRuntimeException(re); - resultErrorLog(threadId, getExecPeriod(startTime), result, re.getMessage(), ExceptionUtils.getErrorStackTrace(re)); + resultErrorLog(transactionSerial, getExecPeriod(startTime), result, re.getMessage(), ExceptionUtils.getErrorStackTrace(re)); } catch (Exception e) { result = handlerException(e); - resultErrorLog(threadId, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); + resultErrorLog(transactionSerial, getExecPeriod(startTime), result, e.getMessage(), ExceptionUtils.getErrorStackTrace(e)); } return result; } /** * info日志 - * @param threadId + * @param transactionSerial * @param execTimeMillis * @param result */ - private void resultInfoLog(Long threadId, Long execTimeMillis, Object result) { - log.info("<<<<<<<<正常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}", - threadId, execTimeMillis, result == null ? result : result.toString()); + private void resultInfoLog(String transactionSerial, Long execTimeMillis, Object result) { + log.info("<<<<<<<<正常响应<<<<<<<<:事务流水号:{}, 执行时长:{}ms, 响应数据:{}", + transactionSerial, execTimeMillis, result == null ? result : result.toString()); } /** * 异常信息 - * @param threadId + * @param transactionSerial * @param execTimeMillis * @param result * @param exceptionMsg * @param exceptionDetail */ - private void resultErrorLog(Long threadId, Long execTimeMillis, Object result, String exceptionMsg, String exceptionDetail) { - log.error("<<<<<<<<异常响应<<<<<<<<:线程ID:{},执行时长:{}ms, 响应数据:{}, 异常信息:{}, 堆栈信息:{}", - threadId, execTimeMillis, result == null ? result : result.toString(), exceptionMsg, exceptionDetail); + private void resultErrorLog(String transactionSerial, Long execTimeMillis, Object result, String exceptionMsg, String exceptionDetail) { + log.error("<<<<<<<<异常响应<<<<<<<<:事务流水号:{}, 执行时长:{}ms, 响应数据:{}, 异常信息:{}, 堆栈信息:{}", + transactionSerial, execTimeMillis, result == null ? result : result.toString(), exceptionMsg, exceptionDetail); } /** diff --git a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/AppClientConstant.java b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/AppClientConstant.java index e8167f2f21..1eca4a088b 100644 --- a/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/AppClientConstant.java +++ b/epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/AppClientConstant.java @@ -40,4 +40,9 @@ public interface AppClientConstant { * 客户端 * */ String CLIENT = "client"; + + /** + * 事务流水号,每次请求串起来的多个服务拥有相同的流水号,便于日志追踪 + */ + String TRANSACTION_SERIAL_KEY = "Transaction-Serial"; } diff --git a/epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java b/epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java index 1da14ce7ef..40a9e363ab 100644 --- a/epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java +++ b/epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java @@ -17,6 +17,8 @@ import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; +import java.util.UUID; + /** * Feign调用发送请求的Filter * 目前用于封装用户相关信息到request,供上游微服务使用 @@ -55,7 +57,9 @@ public class FeignRequestFilter implements GlobalFilter, UserTokenFilter { if (baseTokenDto != null) { ServerHttpRequest build = exchange.getRequest().mutate() - .header(AppClientConstant.USER_ID, new String[]{baseTokenDto.getUserId()}).build(); + .header(AppClientConstant.USER_ID, new String[]{baseTokenDto.getUserId()}) + .header(AppClientConstant.TRANSACTION_SERIAL_KEY, new String[]{UUID.randomUUID().toString()}) + .build(); return chain.filter(exchange.mutate().request(build).build()); }