|
@@ -0,0 +1,67 @@
|
|
|
+package com.zanxiang.game.data.serve.utils;
|
|
|
+
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author tianhua
|
|
|
+ * @time 2023/10/16
|
|
|
+ * @Description Api历史访问记录
|
|
|
+ **/
|
|
|
+@Component
|
|
|
+@Aspect
|
|
|
+public class ApiVisitHistory {
|
|
|
+
|
|
|
+ /*private Logger log = LoggerFactory.getLogger(ApiVisitHistory.class);
|
|
|
+
|
|
|
+ private ThreadLocal<Long> startTime = new ThreadLocal<>();
|
|
|
+
|
|
|
+ *//**
|
|
|
+ * 定义切面
|
|
|
+ * 此处代表com.zanxiang.game.data.serve.controller包下的所有接口都会被统计
|
|
|
+ *//*
|
|
|
+ @Pointcut("execution(* com.zanxiang.game.data.serve.controller..*.*(..)) && !execution(* com.zanxiang.game.data.serve.controller.GameListController.*(..))")
|
|
|
+ public void pointCut(){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ *//**
|
|
|
+ * 在接口原有的方法执行前,将会首先执行此处的代码
|
|
|
+ *//*
|
|
|
+ @Before("pointCut()")
|
|
|
+ public void doBefore(JoinPoint joinPoint) {
|
|
|
+ startTime.set(System.currentTimeMillis());
|
|
|
+ //获取传入目标方法的参数
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ log.info("类名:{}", joinPoint.getSignature().getDeclaringType().getSimpleName());
|
|
|
+ log.info("方法名:{}", joinPoint.getSignature().getName() );
|
|
|
+ log.info("输入的参数:{}", Arrays.toString(joinPoint.getArgs()));
|
|
|
+ }
|
|
|
+
|
|
|
+ *//**
|
|
|
+ * 只有正常返回才会执行此方法
|
|
|
+ * 如果程序执行失败,则不执行此方法
|
|
|
+ *//*
|
|
|
+ @AfterReturning(returning = "returnVal", pointcut = "pointCut()")
|
|
|
+ public void doAfterReturning(JoinPoint joinPoint, Object returnVal) {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ log.info("URI:[{}], 耗费时间:[{}] ms, 访问IP:[{}]",
|
|
|
+ request.getRequestURI(),
|
|
|
+ System.currentTimeMillis() - startTime.get(),
|
|
|
+ request.getRemoteUser());
|
|
|
+ }
|
|
|
+
|
|
|
+ *//**
|
|
|
+ * 当接口报错时执行此方法
|
|
|
+ *//*
|
|
|
+ @AfterThrowing(pointcut = "pointCut()")
|
|
|
+ public void doAfterThrowing(JoinPoint joinPoint) {
|
|
|
+ HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
|
|
|
+ log.info("接口访问失败,URI:[{}], 耗费时间:[{}] ms", request.getRequestURI(), System.currentTimeMillis() - startTime.get());
|
|
|
+ }*/
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+
|