|
@@ -10,6 +10,7 @@ import org.aspectj.lang.annotation.AfterReturning;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.slf4j.MDC;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
@@ -31,6 +32,8 @@ public class WebLogAspect {
|
|
|
@Autowired
|
|
|
private HttpServletRequest request;
|
|
|
|
|
|
+ private long startTime ;
|
|
|
+
|
|
|
|
|
|
@Pointcut("execution(* com.gis.web.controller.*.*(..))")//切入点描述 这个是controller包的切入点
|
|
|
public void controllerLog(){}//签名,可以理解成这个切入点的一个名称
|
|
@@ -38,10 +41,15 @@ public class WebLogAspect {
|
|
|
@Before("controllerLog()") //在切入点的方法run之前要干的
|
|
|
public void logBeforeController(JoinPoint joinPoint) throws Exception {
|
|
|
|
|
|
- // 记录请求内容
|
|
|
- log.warn("start : {}" , request.getRequestURI());
|
|
|
- log.info("request Method : {}" , request.getMethod());
|
|
|
- log.info("request IP : {}" , request.getRemoteAddr());
|
|
|
+ startTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ // 设置链路id, 在logback-spring.xml里用
|
|
|
+ MDC.put("TRACE_ID", startTime+"");
|
|
|
+
|
|
|
+ // 记录下请求内容
|
|
|
+ String remoteAddr = request.getRemoteAddr();
|
|
|
+ log.warn("start : {}, {}, uuid: {}" , request.getMethod(), request.getRequestURI(), startTime);
|
|
|
+ log.info("request IP:{}" , request.getRemoteAddr());
|
|
|
log.info("request Args : {}" , Arrays.toString(joinPoint.getArgs()));
|
|
|
|
|
|
// 获取token
|
|
@@ -77,8 +85,7 @@ public class WebLogAspect {
|
|
|
@AfterReturning(returning = "ret", pointcut = "controllerLog()")
|
|
|
public void doAfterReturning(Object ret) throws Throwable {
|
|
|
// 处理完请求,返回内容
|
|
|
-// log.info("响应RESPONSE : " + ret.toString());
|
|
|
- log.warn("end : {}", request.getRequestURI());
|
|
|
+ log.warn("end: {}, {}, uuid: {}, 响应耗时:{} ms", request.getMethod(), request.getRequestURI(), startTime, (System.currentTimeMillis() - startTime));
|
|
|
}
|
|
|
|
|
|
/**
|