diff --git a/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java b/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java index 4ab43ad84..beb654210 100644 --- a/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java +++ b/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/aop/OperateLogAspect.java @@ -42,21 +42,32 @@ import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstan import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants.SUCCESS; /** - * 拦截使用 @ApiOperation 注解,如果满足条件,则生成操作日志。 + * 拦截使用 @OperateLog 注解,如果满足条件,则生成操作日志。 * 满足如下任一条件,则会进行记录: * 1. 使用 @ApiOperation + 非 @GetMapping * 2. 使用 @OperateLog 注解 * * 但是,如果声明 @OperateLog 注解时,将 enable 属性设置为 false 时,强制不记录。 * - * 为什么考虑使用 @ApiOperation 记录呢?避免有小伙伴忘记添加 @OperateLog 注解 - * * @author 芋道源码 */ @Aspect @Slf4j public class OperateLogAspect { + /** + * 用于记录操作内容的上下文 + * + * @see SysOperateLogCreateReqVO#getContent() + */ + private static final ThreadLocal CONTENT = new ThreadLocal<>(); + /** + * 用于记录拓展字段的上下文 + * + * @see SysOperateLogCreateReqVO#getExts() + */ + private static final ThreadLocal> EXTS = new ThreadLocal<>(); + @Resource private OperateLogFrameworkService operateLogFrameworkService; @@ -84,9 +95,27 @@ public class OperateLogAspect { } catch (Throwable exception) { this.log(joinPoint, operateLog, apiOperation, startTime, null, exception); throw exception; + } finally { + clearThreadLocal(); } } + public static void setContent(String content) { + CONTENT.set(content); + } + + public static void addExt(String key, Object value) { + if (EXTS.get() == null) { + EXTS.set(new HashMap<>()); + } + EXTS.get().put(key, value); + } + + private static void clearThreadLocal() { + CONTENT.remove(); + EXTS.remove(); + } + private void log(ProceedingJoinPoint joinPoint, OperateLog operateLog, ApiOperation apiOperation, Date startTime, Object result, Throwable exception) { try { @@ -154,6 +183,9 @@ public class OperateLogAspect { SysOperateLogTypeEnum operateLogType = convertOperateLogType(requestMethod); operateLogVO.setType(operateLogType != null ? operateLogType.getType() : null); } + // content 和 exts 属性 + operateLogVO.setContent(CONTENT.get()); + operateLogVO.setExts(EXTS.get()); } private static void fillRequestFields(SysOperateLogCreateReqVO operateLogVO) { @@ -199,6 +231,11 @@ public class OperateLogAspect { } } + private static void fillContentFields(SysOperateLogCreateReqVO operateLogVO) { + operateLogVO.setContent(CONTENT.get()); + operateLogVO.setExts(EXTS.get()); + } + private static boolean isLogEnable(ProceedingJoinPoint joinPoint, OperateLog operateLog) { // 有 @OperateLog 注解的情况下 if (operateLog != null) { diff --git a/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/util/OperateLogUtils.java b/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/util/OperateLogUtils.java new file mode 100644 index 000000000..71135eb81 --- /dev/null +++ b/src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/util/OperateLogUtils.java @@ -0,0 +1,21 @@ +package cn.iocoder.dashboard.framework.logger.operatelog.core.util; + +import cn.iocoder.dashboard.framework.logger.operatelog.core.aop.OperateLogAspect; + +/** + * 操作日志工具类 + * 目前主要的作用,是提供给业务代码,记录操作明细和拓展字段 + * + * @author 芋道源码 + */ +public class OperateLogUtils { + + public static void setContent(String content) { + OperateLogAspect.setContent(content); + } + + public static void addExt(String key, Object value) { + OperateLogAspect.addExt(key, value); + } + +}