|
@@ -0,0 +1,72 @@
|
|
|
+package com.ads.common.aop;
|
|
|
+
|
|
|
+import com.ads.business.entity.SysReqLog;
|
|
|
+import com.ads.business.service.SysReqLogService;
|
|
|
+import com.ads.common.util.IPUtils;
|
|
|
+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.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.Date;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Create by HeLongJun on 2022/2/24 17:39
|
|
|
+ *
|
|
|
+ * @author lanrenspace@163.com
|
|
|
+ * @Description:
|
|
|
+ */
|
|
|
+@Aspect
|
|
|
+public class RequestLogAspect {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SysReqLogService reqLogService;
|
|
|
+
|
|
|
+
|
|
|
+ @Pointcut("execution(public * com.ads.business.api.*.*(..)) " +
|
|
|
+ "|| execution(public * com.ads.business.controller.*.*(..))")
|
|
|
+ public void pointcut() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Around("pointcut()")
|
|
|
+ public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
|
+ long startTime = System.currentTimeMillis();
|
|
|
+ Object result = point.proceed();
|
|
|
+ long time = System.currentTimeMillis() - startTime;
|
|
|
+ saveLog(point, time);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void saveLog(ProceedingJoinPoint joinPoint, long time) {
|
|
|
+ MethodSignature signature = (MethodSignature) joinPoint.getSignature();
|
|
|
+ Method method = signature.getMethod();
|
|
|
+ SysReqLog sysLog = new SysReqLog();
|
|
|
+ String className = joinPoint.getTarget().getClass().getName();
|
|
|
+ String methodName = signature.getName();
|
|
|
+ sysLog.setMethod(className + "." + methodName + "()");
|
|
|
+
|
|
|
+ Object[] args = joinPoint.getArgs();
|
|
|
+ LocalVariableTableParameterNameDiscoverer lvpnd = new LocalVariableTableParameterNameDiscoverer();
|
|
|
+ String[] parameterNames = lvpnd.getParameterNames(method);
|
|
|
+ if (args != null && parameterNames != null) {
|
|
|
+ String params = "";
|
|
|
+ for (int i = 0; i < args.length; i++) {
|
|
|
+ params += " " + parameterNames[i] + ": " + args[i];
|
|
|
+ }
|
|
|
+ sysLog.setRequestParam(params);
|
|
|
+ }
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ sysLog.setIp(IPUtils.getIpAddr(attributes.getRequest()));
|
|
|
+ sysLog.setRequestUrl(attributes.getRequest().getRequestURI());
|
|
|
+ sysLog.setCostTime(time);
|
|
|
+ sysLog.setCreateTime(new Date());
|
|
|
+ reqLogService.save(sysLog);
|
|
|
+ }
|
|
|
+}
|