简介
在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====================================>
这样一来就实现了记录接口调用记录、请求参数、响应结果、执行时长与逻辑解耦。