Browse Source

1.修改日志切面和gateway的FeignRequestFilter,增加事务流水号,便于日志追踪

dev_shibei_match
wxz 5 years ago
parent
commit
2782273c64
  1. 44
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/aspect/BaseRequestLogAspect.java
  2. 5
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/constant/AppClientConstant.java
  3. 6
      epmet-gateway/src/main/java/com/epmet/filter/FeignRequestFilter.java

44
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);
}
/**

5
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";
}

6
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());
}

Loading…
Cancel
Save