前言
这篇博客主要是针对我们前期部分接口不稳定和有部分接口需要大量业务沟通接口,针对请求参数和返回参数进行打印。部分接口调用逻辑链过长,不易分析添加同一个请求的参数添加唯一标识,防止并发比较大情况,找不到同一请求接口的上下文信息。
日志打印
1、构建一个注解用于放置在需要打印接口上面
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface LogAnnotation {
boolean printParam() default true;
}
2、设置针对打印注解的controller类进行切面
@Component
@Aspect
@Log4j2
public class RequestLogAspect {
@Pointcut(value = "execution(* com.yin.databaseproject.controller..*(..)) && @annotation(logAnnotation)")
public void request(LogAnnotation logAnnotation) {
}
@Around("request(logAnnotation)")
public Object doAround(ProceedingJoinPoint proceedingJoinPoint,LogAnnotation logAnnotation) throws Throwable {
long start = System.currentTimeMillis();
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
Object result = proceedingJoinPoint.proceed();
RequestInfo requestInfo = new RequestInfo();
requestInfo.setIp(IpUtil.getClientIp(request));
requestInfo.setUrl(request.getRequestURL().toString());
requestInfo.setHttpMethod(request.getMethod());
requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature(