|
@@ -1,10 +1,8 @@
|
|
|
package com.lc.ibps.aop;
|
|
package com.lc.ibps.aop;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.StopWatch;
|
|
import cn.hutool.core.date.StopWatch;
|
|
|
-import cn.hutool.json.JSONUtil;
|
|
|
|
|
-import com.lc.ibps.base.web.context.ContextUtil;
|
|
|
|
|
-import com.lc.ibps.cloud.utils.RequestUtil;
|
|
|
|
|
import com.lc.ibps.sysdata.dao.UpdateDataTableDao;
|
|
import com.lc.ibps.sysdata.dao.UpdateDataTableDao;
|
|
|
|
|
+import com.lc.ibps.untils.LogAopUtil;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
@@ -13,12 +11,10 @@ import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
-import javax.servlet.ServletRequest;
|
|
|
|
|
-import javax.servlet.ServletResponse;
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
-import java.util.*;
|
|
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
-import java.util.stream.Stream;
|
|
|
|
|
|
|
+import java.io.StringWriter;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.Objects;
|
|
|
|
|
|
|
|
|
|
|
|
|
@Aspect
|
|
@Aspect
|
|
@@ -35,29 +31,19 @@ public class UpdateDBLogAspect {
|
|
|
@Around("execution(* com.lc.ibps.sysdata.controller.UpdateDataTableController.*(..))")
|
|
@Around("execution(* com.lc.ibps.sysdata.controller.UpdateDataTableController.*(..))")
|
|
|
public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
public Object around(ProceedingJoinPoint point) throws Throwable {
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
|
HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
|
|
|
- String methodName = point.getSignature().getName();
|
|
|
|
|
- // 执行目标方法
|
|
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
stopWatch.start();
|
|
stopWatch.start();
|
|
|
- Object result = point.proceed();
|
|
|
|
|
- stopWatch.stop();
|
|
|
|
|
- if (ifAddLog(methodName)){
|
|
|
|
|
- List<Object> paramList = Stream.of(point.getArgs())
|
|
|
|
|
- .filter(args -> !(args instanceof ServletRequest))
|
|
|
|
|
- .filter(args -> !(args instanceof ServletResponse))
|
|
|
|
|
- .collect(Collectors.toList());
|
|
|
|
|
- String printParamStr = paramList.size() == 1 ? JSONUtil.toJsonStr(paramList.get(0)) : JSONUtil.toJsonStr(paramList);
|
|
|
|
|
- // 记录操作日志
|
|
|
|
|
- Map<String,Object> map = new HashMap<>();
|
|
|
|
|
- map.put("method_", methodName);
|
|
|
|
|
- map.put("params_", printParamStr);
|
|
|
|
|
- map.put("user_id_", ContextUtil.getCurrentUser().getUserId());
|
|
|
|
|
- map.put("user_name_", ContextUtil.getCurrentUser().getFullname());
|
|
|
|
|
- map.put("time_", stopWatch.getTotalTimeMillis());
|
|
|
|
|
- map.put("ip_", RequestUtil.getIpAddr(request));
|
|
|
|
|
- map.put("response_", JSONUtil.toJsonStr(result));
|
|
|
|
|
- map.put("uri_", request.getRequestURI());
|
|
|
|
|
- updateDataTableDao.insertLog(map);
|
|
|
|
|
|
|
+ Object result = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ result = point.proceed();
|
|
|
|
|
+ } catch (Throwable throwable) {
|
|
|
|
|
+ updateDataTableDao.insertLog(LogAopUtil.logError(request, point, throwable));
|
|
|
|
|
+ throw throwable;
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ stopWatch.stop();
|
|
|
|
|
+ }
|
|
|
|
|
+ if (ifAddLog(point.getSignature().getName())){
|
|
|
|
|
+ updateDataTableDao.insertLog(LogAopUtil.log( request, point, result, stopWatch));
|
|
|
}
|
|
}
|
|
|
return result;
|
|
return result;
|
|
|
}
|
|
}
|
|
@@ -65,7 +51,7 @@ public class UpdateDBLogAspect {
|
|
|
public boolean ifAddLog(String methodName){
|
|
public boolean ifAddLog(String methodName){
|
|
|
String[] names = {"addDataContextTable","updateDataContextTable","updatesDatasContextTable",
|
|
String[] names = {"addDataContextTable","updateDataContextTable","updatesDatasContextTable",
|
|
|
"updatesBatchContextTable","batchDelete","deleteDataContextTable"};
|
|
"updatesBatchContextTable","batchDelete","deleteDataContextTable"};
|
|
|
- return Arrays.stream(names).anyMatch(s -> s.equals(methodName));
|
|
|
|
|
|
|
+ return Arrays.asList(names).contains(methodName);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
}
|