From 2fccaa2b9a189a9892e07f10ed52deccaaa88221 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sun, 17 Jan 2021 10:44:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=93=8D=E4=BD=9C=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../operatelog/core/aop/OperateLogAspect.java | 43 +++++++++++++++++-- .../operatelog/core/util/OperateLogUtils.java | 21 +++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/util/OperateLogUtils.java 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); + } + +}