Java排查接口调用记录、请求参数、响应结果、执行时长

简介

  • 在Web应用当中记录接口调用记录、请求参数、响应结果、执行时长是再常见不过的事情了,记录的方式有很多。
  • 最近阅读之前的历史代码发现以前一些接口的统计时长、记录调用记录、请求参数都是合代码逻辑并存,严重影响了代码可读性。
  • 而记录接口调用记录、请求参数、响应结果、执行时长这些操作完全可以抽象出来,用 AOP 横向拦截去处理,实现解耦、提高代码可读可维护性。这篇文章将介绍如何实现。

实现思路

代码实现

废话不多说,直接上代码。

定义@AroundLog注解,用于标识需要记录的接口。

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
@Documented
public @interface AroundLog {
    String flag() default "";
}

切面实现

@Aspect
@Slf4j
@Component
public class AroundLogAspect {

    @Around("@annotation(aroundLog)")
    public Object aroundLog(ProceedingJoinPoint point, AroundLog aroundLog) {

        StringBuilder sb = new StringBuilder();
        StopWatch started = new StopWatch();

        try {
            MethodSignature signature = (MethodSignature) point.getSignature();
            Method method = signature.getMethod();

            sb.append("\n<===================================START===================================>\n");
            sb.append("call time:>").append(LocalDateTime.now()).append("\n");
            ObjectMapper mapper = new ObjectMapper();

            String methodName = method.getName();
            sb.append("method name:> ").append(methodName).append("\n");

            sb.append("log flag:> ").append(aroundLog.flag()).append("\n");

            Parameter[] parameters = method.getParameters();
            Object[] args = point.getArgs();
            sb.append("request:>\n");
            if (args != null) {
                for (int i = 0; i < args.length; i++) {
                    String paramName = parameters[i].getName();
                    String argStr= null;
                    try {
                        argStr  = mapper.writeValueAsString(args[i]);
                    } catch (Exception e) {
                        argStr = "无法解析";
                    }
                    sb.append(paramName).append(":").append(argStr).append("\n");
                }
            }

            started.start();
            Object proceed = point.proceed();

            sb.append("response:>\n").append(mapper.writeValueAsString(proceed)).append("\n");

            return proceed;
        } catch (RuntimeException e) {
            sb.append("RuntimeException:>").append(e.getMessage()).append("\n");
            throw e;
        } catch (Throwable throwable) {
            sb.append("Throwable:>").append(throwable.getMessage()).append("\n");
            throw new RuntimeException("系统异常!");
        }finally {
            started.stop();
            sb.append("call total time(ms) :> ").append(started.getTime()).append("\n");
            sb.append("<====================================END====================================>\n");
            log.info(sb.toString());
        }
    }
}

调用接口测试,日志输出如下

<===================================START===================================>
call time:>2021-08-30T19:34:47.062
methodName:>pay
log flag:> 
request:>
response:>
{"status":true,"code":null,"message":"success","errorMsg":null,"data":null}
call total time(ms) :>21
<====================================END====================================>

这样一来就实现了记录接口调用记录、请求参数、响应结果、执行时长与逻辑解耦。

  • 原文链接:https://blog.csdn.net/weixin_43950568/article/details/120003896
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值