common-trace模块:common的pom.xml中加上<modules>
This commit is contained in:
parent
0a00c818db
commit
e1723c511b
@ -41,6 +41,11 @@
|
|||||||
<artifactId>commons-io</artifactId>
|
<artifactId>commons-io</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjrt</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@ -0,0 +1,49 @@
|
|||||||
|
package com.axzo.framework.trace.interceptor;
|
||||||
|
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.axzo.framework.trace.util.RequestUtil;
|
||||||
|
import lombok.SneakyThrows;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.StopWatch;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打印出入参及耗时
|
||||||
|
*
|
||||||
|
* @author TanJ
|
||||||
|
* @date 2021/5/12
|
||||||
|
*/
|
||||||
|
@Aspect
|
||||||
|
@Slf4j
|
||||||
|
@Component
|
||||||
|
public class RequestLog {
|
||||||
|
|
||||||
|
final String checkDeath = "checkDeath";
|
||||||
|
|
||||||
|
@Around("@within(restController)||@annotation(restController)")
|
||||||
|
@SneakyThrows
|
||||||
|
public Object request(ProceedingJoinPoint joinPoint, RestController restController) {
|
||||||
|
|
||||||
|
HttpServletRequest request = RequestUtil.getRequest();
|
||||||
|
|
||||||
|
if (request == null) {
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
if (request.getRequestURL().toString().contains(checkDeath)) {
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
StopWatch stopWatch = new StopWatch();
|
||||||
|
stopWatch.start();
|
||||||
|
Object proceed = joinPoint.proceed();
|
||||||
|
stopWatch.stop();
|
||||||
|
log.info("[response]返回记录:responseParam = {} latency = {}", JSONUtil.toJsonStr(proceed),
|
||||||
|
stopWatch.getTotalTimeMillis());
|
||||||
|
return proceed;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,58 @@
|
|||||||
|
package com.axzo.framework.trace.util;
|
||||||
|
|
||||||
|
import org.springframework.web.context.request.RequestAttributes;
|
||||||
|
import org.springframework.web.context.request.RequestContextHolder;
|
||||||
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author TanJ
|
||||||
|
* @date 2021/5/24
|
||||||
|
*/
|
||||||
|
public class RequestUtil {
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getRequest
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static HttpServletRequest getRequest() {
|
||||||
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||||
|
if (requestAttributes!=null) {
|
||||||
|
return
|
||||||
|
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
||||||
|
.getRequest();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* getResponse
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static HttpServletResponse getResponse() {
|
||||||
|
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||||
|
if (null != requestAttributes) {
|
||||||
|
return
|
||||||
|
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
||||||
|
.getResponse();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* setInheritableHolder
|
||||||
|
*/
|
||||||
|
public static void setInheritableHolder() {
|
||||||
|
RequestContextHolder.setRequestAttributes(RequestContextHolder.getRequestAttributes(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user