|
@@ -1,244 +0,0 @@
|
|
|
-package com.middle.platform.log.core.aop;
|
|
|
|
|
-
|
|
|
|
|
-import cn.hutool.core.date.LocalDateTimeUtil;
|
|
|
|
|
-import cn.hutool.core.exceptions.ExceptionUtil;
|
|
|
|
|
-import cn.hutool.core.util.ArrayUtil;
|
|
|
|
|
-import com.google.common.collect.Maps;
|
|
|
|
|
-import com.middle.platform.common.core.constant.Global;
|
|
|
|
|
-import com.middle.platform.common.core.utils.JsonUtils;
|
|
|
|
|
-import com.middle.platform.common.core.utils.Result;
|
|
|
|
|
-import com.middle.platform.common.core.utils.ServletUtils;
|
|
|
|
|
-import com.middle.platform.log.core.annotations.OperateLog;
|
|
|
|
|
-import com.middle.platform.log.core.enums.OperateTypeEnum;
|
|
|
|
|
-import com.middle.platform.log.core.service.LogFrameworkService;
|
|
|
|
|
-import com.middle.platform.log.core.service.OperateLogDto;
|
|
|
|
|
-import jakarta.annotation.Resource;
|
|
|
|
|
-import jakarta.servlet.http.HttpServletRequest;
|
|
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
|
|
-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.reflect.MethodSignature;
|
|
|
|
|
-import org.springframework.core.annotation.AnnotationUtils;
|
|
|
|
|
-import org.springframework.validation.BindingResult;
|
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
-import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
|
|
-import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
-import java.lang.reflect.Array;
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.Collection;
|
|
|
|
|
-import java.util.Map;
|
|
|
|
|
-import java.util.function.Predicate;
|
|
|
|
|
-import java.util.stream.IntStream;
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-@Aspect
|
|
|
|
|
-@Slf4j
|
|
|
|
|
-public class OperateLogAspect {
|
|
|
|
|
-
|
|
|
|
|
- @Resource
|
|
|
|
|
- private LogFrameworkService logFrameworkService;
|
|
|
|
|
-
|
|
|
|
|
- @Around("@annotation(operateLog)")
|
|
|
|
|
- public Object around(ProceedingJoinPoint joinPoint, OperateLog operateLog) throws Throwable {
|
|
|
|
|
- return around0(joinPoint, operateLog);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private Object around0(ProceedingJoinPoint joinPoint, OperateLog operateLog) throws Throwable {
|
|
|
|
|
- // 记录开始时间
|
|
|
|
|
- LocalDateTime startTime = LocalDateTime.now();
|
|
|
|
|
- try {
|
|
|
|
|
- // 执行原有方法
|
|
|
|
|
- Object result = joinPoint.proceed();
|
|
|
|
|
- // 记录正常执行时的操作日志
|
|
|
|
|
- this.log(joinPoint, operateLog, startTime, result, null);
|
|
|
|
|
- return result;
|
|
|
|
|
- } catch (Throwable exception) {
|
|
|
|
|
- this.log(joinPoint, operateLog, startTime, null, exception);
|
|
|
|
|
- throw exception;
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private void log(ProceedingJoinPoint joinPoint, OperateLog operateLog, LocalDateTime startTime, Object result, Throwable exception) {
|
|
|
|
|
- try {
|
|
|
|
|
- // 判断不记录的情况
|
|
|
|
|
- if (!isLogEnable(joinPoint, operateLog)) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- // 真正记录操作日志
|
|
|
|
|
- this.log0(joinPoint, operateLog, startTime, result, exception);
|
|
|
|
|
- } catch (Throwable ex) {
|
|
|
|
|
- log.error("[log][记录操作日志时,发生异常,其中参数是 joinPoint({}) operateLog({}) result({}) exception({}) ]", joinPoint, operateLog, result, exception, ex);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private void log0(ProceedingJoinPoint joinPoint, OperateLog operateLog, LocalDateTime startTime, Object result, Throwable exception) {
|
|
|
|
|
- OperateLogDto operateLogObj = new OperateLogDto();
|
|
|
|
|
- // 补全通用字段
|
|
|
|
|
-// operateLogObj.setTraceId(TracerUtils.getTraceId());
|
|
|
|
|
- operateLogObj.setStartTime(startTime);
|
|
|
|
|
- // 补充用户信息
|
|
|
|
|
- fillUserFields(operateLogObj);
|
|
|
|
|
- // 补全模块信息
|
|
|
|
|
- fillModuleFields(operateLogObj, joinPoint, operateLog);
|
|
|
|
|
- // 补全请求信息
|
|
|
|
|
- fillRequestFields(operateLogObj);
|
|
|
|
|
- // 补全方法信息
|
|
|
|
|
- fillMethodFields(operateLogObj, joinPoint, operateLog, startTime, result, exception);
|
|
|
|
|
-
|
|
|
|
|
- // 异步记录日志
|
|
|
|
|
- logFrameworkService.createOperateLog(operateLogObj);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void fillUserFields(OperateLogDto operateLogObj) {
|
|
|
|
|
-// operateLogObj.setUserId(WebFrameworkUtils.getLoginUserId());
|
|
|
|
|
-// operateLogObj.setUserType(WebFrameworkUtils.getLoginUserType());
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void fillModuleFields(OperateLogDto operateLogObj, ProceedingJoinPoint joinPoint, OperateLog operateLog) {
|
|
|
|
|
- // module 属性
|
|
|
|
|
- if (operateLog != null) {
|
|
|
|
|
- operateLogObj.setModule(operateLog.module());
|
|
|
|
|
- }
|
|
|
|
|
- // name 属性
|
|
|
|
|
- if (operateLog != null) {
|
|
|
|
|
- operateLogObj.setName(operateLog.name());
|
|
|
|
|
- }
|
|
|
|
|
- // type 属性
|
|
|
|
|
- if (operateLog != null && ArrayUtil.isNotEmpty(operateLog.type())) {
|
|
|
|
|
- operateLogObj.setType(operateLog.type()[0].getType());
|
|
|
|
|
- }
|
|
|
|
|
- if (operateLogObj.getType() == null) {
|
|
|
|
|
- RequestMethod requestMethod = obtainFirstMatchRequestMethod(obtainRequestMethod(joinPoint));
|
|
|
|
|
- OperateTypeEnum operateLogType = convertOperateLogType(requestMethod);
|
|
|
|
|
- operateLogObj.setType(operateLogType != null ? operateLogType.getType() : null);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void fillRequestFields(OperateLogDto operateLogObj) {
|
|
|
|
|
- // 获得 Request 对象
|
|
|
|
|
- HttpServletRequest request = ServletUtils.getRequest();
|
|
|
|
|
- if (request == null) {
|
|
|
|
|
- return;
|
|
|
|
|
- }
|
|
|
|
|
- // 补全请求信息
|
|
|
|
|
- operateLogObj.setRequestMethod(request.getMethod());
|
|
|
|
|
- operateLogObj.setRequestUrl(request.getRequestURI());
|
|
|
|
|
- operateLogObj.setUserIp(ServletUtils.getClientIP(request));
|
|
|
|
|
- operateLogObj.setUserAgent(ServletUtils.getUserAgent(request));
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static void fillMethodFields(OperateLogDto operateLogObj, ProceedingJoinPoint joinPoint, OperateLog operateLog, LocalDateTime startTime, Object result, Throwable exception) {
|
|
|
|
|
- MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
|
|
|
- operateLogObj.setJavaMethod(methodSignature.toString());
|
|
|
|
|
- if (operateLog == null || operateLog.logArgs()) {
|
|
|
|
|
- operateLogObj.setJavaMethodArgs(obtainMethodArgs(joinPoint));
|
|
|
|
|
- }
|
|
|
|
|
- if (operateLog == null || operateLog.logResultData()) {
|
|
|
|
|
- operateLogObj.setResultData(String.valueOf(result));
|
|
|
|
|
- }
|
|
|
|
|
- operateLogObj.setDuration((int) (LocalDateTimeUtil.between(startTime, LocalDateTime.now()).toMillis()));
|
|
|
|
|
- // (正常)处理 resultCode 和 resultMsg 字段
|
|
|
|
|
- if (result instanceof Result<?> commonResult) {
|
|
|
|
|
- operateLogObj.setResultCode(commonResult.getCode());
|
|
|
|
|
- operateLogObj.setResultMsg(commonResult.getMsg());
|
|
|
|
|
- } else {
|
|
|
|
|
- operateLogObj.setResultCode(Global.SUCCESS);
|
|
|
|
|
- }
|
|
|
|
|
- // (异常)处理 resultCode 和 resultMsg 字段
|
|
|
|
|
- if (exception != null) {
|
|
|
|
|
- operateLogObj.setResultCode(Global.ERROR);
|
|
|
|
|
- operateLogObj.setResultMsg(ExceptionUtil.getRootCauseMessage(exception));
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static boolean isLogEnable(ProceedingJoinPoint joinPoint, OperateLog operateLog) {
|
|
|
|
|
- // 有 @OperateLog 注解的情况下
|
|
|
|
|
- if (operateLog != null) {
|
|
|
|
|
- return operateLog.enable();
|
|
|
|
|
- }
|
|
|
|
|
- return obtainFirstLogRequestMethod(obtainRequestMethod(joinPoint)) != null;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static RequestMethod obtainFirstLogRequestMethod(RequestMethod[] requestMethods) {
|
|
|
|
|
- if (ArrayUtil.isEmpty(requestMethods)) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- return Arrays.stream(requestMethods).filter(requestMethod -> requestMethod == RequestMethod.POST || requestMethod == RequestMethod.PUT || requestMethod == RequestMethod.DELETE).findFirst().orElse(null);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static RequestMethod obtainFirstMatchRequestMethod(RequestMethod[] requestMethods) {
|
|
|
|
|
- if (ArrayUtil.isEmpty(requestMethods)) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- // 优先,匹配最优的 POST、PUT、DELETE
|
|
|
|
|
- RequestMethod result = obtainFirstLogRequestMethod(requestMethods);
|
|
|
|
|
- if (result != null) {
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- // 然后,匹配次优的 GET
|
|
|
|
|
- result = Arrays.stream(requestMethods).filter(requestMethod -> requestMethod == RequestMethod.GET).findFirst().orElse(null);
|
|
|
|
|
- if (result != null) {
|
|
|
|
|
- return result;
|
|
|
|
|
- }
|
|
|
|
|
- // 兜底,获得第一个
|
|
|
|
|
- return requestMethods[0];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static OperateTypeEnum convertOperateLogType(RequestMethod requestMethod) {
|
|
|
|
|
- if (requestMethod == null) {
|
|
|
|
|
- return null;
|
|
|
|
|
- }
|
|
|
|
|
- return switch (requestMethod) {
|
|
|
|
|
- case GET -> OperateTypeEnum.GET;
|
|
|
|
|
- case POST -> OperateTypeEnum.CREATE;
|
|
|
|
|
- case PUT -> OperateTypeEnum.UPDATE;
|
|
|
|
|
- case DELETE -> OperateTypeEnum.DELETE;
|
|
|
|
|
- default -> OperateTypeEnum.OTHER;
|
|
|
|
|
- };
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static RequestMethod[] obtainRequestMethod(ProceedingJoinPoint joinPoint) {
|
|
|
|
|
- RequestMapping requestMapping = AnnotationUtils.getAnnotation( // 使用 Spring 的工具类,可以处理 @RequestMapping 别名注解
|
|
|
|
|
- ((MethodSignature) joinPoint.getSignature()).getMethod(), RequestMapping.class);
|
|
|
|
|
- return requestMapping != null ? requestMapping.method() : new RequestMethod[]{};
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static String obtainMethodArgs(ProceedingJoinPoint joinPoint) {
|
|
|
|
|
- MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
|
|
|
|
- String[] argNames = methodSignature.getParameterNames();
|
|
|
|
|
- Object[] argValues = joinPoint.getArgs();
|
|
|
|
|
- // 拼接参数
|
|
|
|
|
- Map<String, Object> args = Maps.newHashMapWithExpectedSize(argValues.length);
|
|
|
|
|
- for (int i = 0; i < argNames.length; i++) {
|
|
|
|
|
- String argName = argNames[i];
|
|
|
|
|
- Object argValue = argValues[i];
|
|
|
|
|
- // 被忽略时,标记为 ignore 字符串,避免和 null 混在一起
|
|
|
|
|
- args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]");
|
|
|
|
|
- }
|
|
|
|
|
- return JsonUtils.toJsonString(args);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- private static boolean isIgnoreArgs(Object object) {
|
|
|
|
|
- Class<?> clazz = object.getClass();
|
|
|
|
|
- // 处理数组的情况
|
|
|
|
|
- if (clazz.isArray()) {
|
|
|
|
|
- return IntStream.range(0, Array.getLength(object)).anyMatch(index -> isIgnoreArgs(Array.get(object, index)));
|
|
|
|
|
- }
|
|
|
|
|
- // 递归,处理数组、Collection、Map 的情况
|
|
|
|
|
- if (Collection.class.isAssignableFrom(clazz)) {
|
|
|
|
|
- return ((Collection<?>) object).stream().anyMatch((Predicate<Object>) OperateLogAspect::isIgnoreArgs);
|
|
|
|
|
- }
|
|
|
|
|
- if (Map.class.isAssignableFrom(clazz)) {
|
|
|
|
|
- return isIgnoreArgs(((Map<?, ?>) object).values());
|
|
|
|
|
- }
|
|
|
|
|
- // obj
|
|
|
|
|
- return object instanceof MultipartFile || object instanceof HttpServletRequest || object instanceof HttpServletResponse || object instanceof BindingResult;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-}
|
|
|