Commit 2fccaa2b9a189a9892e07f10ed52deccaaa88221

Authored by YunaiV
1 parent 30076f64

完善操作日志

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 @@ -42,21 +42,32 @@ import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstan
42 import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants.SUCCESS; 42 import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants.SUCCESS;
43 43
44 /** 44 /**
45 - * 拦截使用 @ApiOperation 注解,如果满足条件,则生成操作日志。 45 + * 拦截使用 @OperateLog 注解,如果满足条件,则生成操作日志。
46 * 满足如下任一条件,则会进行记录: 46 * 满足如下任一条件,则会进行记录:
47 * 1. 使用 @ApiOperation + 非 @GetMapping 47 * 1. 使用 @ApiOperation + 非 @GetMapping
48 * 2. 使用 @OperateLog 注解 48 * 2. 使用 @OperateLog 注解
49 * 49 *
50 * 但是,如果声明 @OperateLog 注解时,将 enable 属性设置为 false 时,强制不记录。 50 * 但是,如果声明 @OperateLog 注解时,将 enable 属性设置为 false 时,强制不记录。
51 * 51 *
52 - * 为什么考虑使用 @ApiOperation 记录呢?避免有小伙伴忘记添加 @OperateLog 注解  
53 - *  
54 * @author 芋道源码 52 * @author 芋道源码
55 */ 53 */
56 @Aspect 54 @Aspect
57 @Slf4j 55 @Slf4j
58 public class OperateLogAspect { 56 public class OperateLogAspect {
59 57
  58 + /**
  59 + * 用于记录操作内容的上下文
  60 + *
  61 + * @see SysOperateLogCreateReqVO#getContent()
  62 + */
  63 + private static final ThreadLocal<String> CONTENT = new ThreadLocal<>();
  64 + /**
  65 + * 用于记录拓展字段的上下文
  66 + *
  67 + * @see SysOperateLogCreateReqVO#getExts()
  68 + */
  69 + private static final ThreadLocal<Map<String, Object>> EXTS = new ThreadLocal<>();
  70 +
60 @Resource 71 @Resource
61 private OperateLogFrameworkService operateLogFrameworkService; 72 private OperateLogFrameworkService operateLogFrameworkService;
62 73
@@ -84,9 +95,27 @@ public class OperateLogAspect { @@ -84,9 +95,27 @@ public class OperateLogAspect {
84 } catch (Throwable exception) { 95 } catch (Throwable exception) {
85 this.log(joinPoint, operateLog, apiOperation, startTime, null, exception); 96 this.log(joinPoint, operateLog, apiOperation, startTime, null, exception);
86 throw exception; 97 throw exception;
  98 + } finally {
  99 + clearThreadLocal();
87 } 100 }
88 } 101 }
89 102
  103 + public static void setContent(String content) {
  104 + CONTENT.set(content);
  105 + }
  106 +
  107 + public static void addExt(String key, Object value) {
  108 + if (EXTS.get() == null) {
  109 + EXTS.set(new HashMap<>());
  110 + }
  111 + EXTS.get().put(key, value);
  112 + }
  113 +
  114 + private static void clearThreadLocal() {
  115 + CONTENT.remove();
  116 + EXTS.remove();
  117 + }
  118 +
90 private void log(ProceedingJoinPoint joinPoint, OperateLog operateLog, ApiOperation apiOperation, 119 private void log(ProceedingJoinPoint joinPoint, OperateLog operateLog, ApiOperation apiOperation,
91 Date startTime, Object result, Throwable exception) { 120 Date startTime, Object result, Throwable exception) {
92 try { 121 try {
@@ -154,6 +183,9 @@ public class OperateLogAspect { @@ -154,6 +183,9 @@ public class OperateLogAspect {
154 SysOperateLogTypeEnum operateLogType = convertOperateLogType(requestMethod); 183 SysOperateLogTypeEnum operateLogType = convertOperateLogType(requestMethod);
155 operateLogVO.setType(operateLogType != null ? operateLogType.getType() : null); 184 operateLogVO.setType(operateLogType != null ? operateLogType.getType() : null);
156 } 185 }
  186 + // content 和 exts 属性
  187 + operateLogVO.setContent(CONTENT.get());
  188 + operateLogVO.setExts(EXTS.get());
157 } 189 }
158 190
159 private static void fillRequestFields(SysOperateLogCreateReqVO operateLogVO) { 191 private static void fillRequestFields(SysOperateLogCreateReqVO operateLogVO) {
@@ -199,6 +231,11 @@ public class OperateLogAspect { @@ -199,6 +231,11 @@ public class OperateLogAspect {
199 } 231 }
200 } 232 }
201 233
  234 + private static void fillContentFields(SysOperateLogCreateReqVO operateLogVO) {
  235 + operateLogVO.setContent(CONTENT.get());
  236 + operateLogVO.setExts(EXTS.get());
  237 + }
  238 +
202 private static boolean isLogEnable(ProceedingJoinPoint joinPoint, OperateLog operateLog) { 239 private static boolean isLogEnable(ProceedingJoinPoint joinPoint, OperateLog operateLog) {
203 // 有 @OperateLog 注解的情况下 240 // 有 @OperateLog 注解的情况下
204 if (operateLog != null) { 241 if (operateLog != null) {
src/main/java/cn/iocoder/dashboard/framework/logger/operatelog/core/util/OperateLogUtils.java 0 → 100644
  1 +package cn.iocoder.dashboard.framework.logger.operatelog.core.util;
  2 +
  3 +import cn.iocoder.dashboard.framework.logger.operatelog.core.aop.OperateLogAspect;
  4 +
  5 +/**
  6 + * 操作日志工具类
  7 + * 目前主要的作用,是提供给业务代码,记录操作明细和拓展字段
  8 + *
  9 + * @author 芋道源码
  10 + */
  11 +public class OperateLogUtils {
  12 +
  13 + public static void setContent(String content) {
  14 + OperateLogAspect.setContent(content);
  15 + }
  16 +
  17 + public static void addExt(String key, Object value) {
  18 + OperateLogAspect.addExt(key, value);
  19 + }
  20 +
  21 +}