问题定位切面和日志上打印请求唯一log标识

前言

这篇博客主要是针对我们前期部分接口不稳定和有部分接口需要大量业务沟通接口,针对请求参数和返回参数进行打印。部分接口调用逻辑链过长,不易分析添加同一个请求的参数添加唯一标识,防止并发比较大情况,找不到同一请求接口的上下文信息。

日志打印

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(
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值