common-trace模块:common的pom.xml中加上<modules>

This commit is contained in:
xudawei 2024-05-16 11:19:36 +08:00
parent 0a00c818db
commit e1723c511b
3 changed files with 112 additions and 0 deletions

View File

@ -41,6 +41,11 @@
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -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;
}
}

View File

@ -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);
}
}