|
@@ -0,0 +1,67 @@
|
|
|
+package com.szwl.common.aop;
|
|
|
+
|
|
|
+import com.gexin.fastjson.JSON;
|
|
|
+import com.szwl.model.utils.HttpContextUtils;
|
|
|
+import com.szwl.model.utils.IpUtils;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
+import org.aspectj.lang.annotation.Around;
|
|
|
+import org.aspectj.lang.annotation.Aspect;
|
|
|
+import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.aspectj.lang.reflect.MethodSignature;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.reflect.Method;
|
|
|
+
|
|
|
+@Aspect // 切面 定义了通知和切点的关系
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class LogAspect {
|
|
|
+
|
|
|
+ // 表示切点加在哪里
|
|
|
+ @Pointcut("@annotation(com.szwl.common.aop.LogAnnotation)")
|
|
|
+ public void pt() {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Around("pt()")
|
|
|
+ public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
|
+
|
|
|
+ long beginTime = System.currentTimeMillis();
|
|
|
+
|
|
|
+ Object returnValue = joinPoint.proceed();
|
|
|
+
|
|
|
+ long time = System.currentTimeMillis() - beginTime;
|
|
|
+
|
|
|
+ // 保存日志
|
|
|
+ recordLog(joinPoint, time);
|
|
|
+
|
|
|
+ return returnValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void recordLog(ProceedingJoinPoint joinPoint, long time) {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+ LogAnnotation logAnnotation = method.getAnnotation(LogAnnotation.class);
|
|
|
+ log.info("========log start=========");
|
|
|
+ log.info("module:{}", logAnnotation.module());
|
|
|
+ log.info("operation:{}", logAnnotation.operator());
|
|
|
+
|
|
|
+ // 请求的方法名
|
|
|
+ String className = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = signature.getName();
|
|
|
+ log.info("request method:{}", className + "." + methodName + "()");
|
|
|
+
|
|
|
+ // 请求的参数
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ String params = JSON.toJSONString(args[0]);
|
|
|
+ log.info("params:{}", params);
|
|
|
+
|
|
|
+ // 获取request 设置ip地址
|
|
|
+ HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
|
|
|
+ log.info("ip:{}", IpUtils.getIpAddr(request));
|
|
|
+
|
|
|
+ log.info("execute time : {} ms", time);
|
|
|
+ log.info("==========log end==========");
|
|
|
+ }
|
|
|
+}
|