完善操作日志

This commit is contained in:
YunaiV 2021-01-17 10:44:07 +08:00
parent 30076f6472
commit 2fccaa2b9a
2 changed files with 61 additions and 3 deletions

View File

@ -42,21 +42,32 @@ import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstan
import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants.SUCCESS; import static cn.iocoder.dashboard.common.exception.enums.GlobalErrorCodeConstants.SUCCESS;
/** /**
* 拦截使用 @ApiOperation 注解如果满足条件则生成操作日志 * 拦截使用 @OperateLog 注解如果满足条件则生成操作日志
* 满足如下任一条件则会进行记录 * 满足如下任一条件则会进行记录
* 1. 使用 @ApiOperation + @GetMapping * 1. 使用 @ApiOperation + @GetMapping
* 2. 使用 @OperateLog 注解 * 2. 使用 @OperateLog 注解
* *
* 但是如果声明 @OperateLog 注解时 enable 属性设置为 false 强制不记录 * 但是如果声明 @OperateLog 注解时 enable 属性设置为 false 强制不记录
* *
* 为什么考虑使用 @ApiOperation 记录呢避免有小伙伴忘记添加 @OperateLog 注解
*
* @author 芋道源码 * @author 芋道源码
*/ */
@Aspect @Aspect
@Slf4j @Slf4j
public class OperateLogAspect { public class OperateLogAspect {
/**
* 用于记录操作内容的上下文
*
* @see SysOperateLogCreateReqVO#getContent()
*/
private static final ThreadLocal<String> CONTENT = new ThreadLocal<>();
/**
* 用于记录拓展字段的上下文
*
* @see SysOperateLogCreateReqVO#getExts()
*/
private static final ThreadLocal<Map<String, Object>> EXTS = new ThreadLocal<>();
@Resource @Resource
private OperateLogFrameworkService operateLogFrameworkService; private OperateLogFrameworkService operateLogFrameworkService;
@ -84,9 +95,27 @@ public class OperateLogAspect {
} catch (Throwable exception) { } catch (Throwable exception) {
this.log(joinPoint, operateLog, apiOperation, startTime, null, exception); this.log(joinPoint, operateLog, apiOperation, startTime, null, exception);
throw 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, private void log(ProceedingJoinPoint joinPoint, OperateLog operateLog, ApiOperation apiOperation,
Date startTime, Object result, Throwable exception) { Date startTime, Object result, Throwable exception) {
try { try {
@ -154,6 +183,9 @@ public class OperateLogAspect {
SysOperateLogTypeEnum operateLogType = convertOperateLogType(requestMethod); SysOperateLogTypeEnum operateLogType = convertOperateLogType(requestMethod);
operateLogVO.setType(operateLogType != null ? operateLogType.getType() : null); operateLogVO.setType(operateLogType != null ? operateLogType.getType() : null);
} }
// content exts 属性
operateLogVO.setContent(CONTENT.get());
operateLogVO.setExts(EXTS.get());
} }
private static void fillRequestFields(SysOperateLogCreateReqVO operateLogVO) { 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) { private static boolean isLogEnable(ProceedingJoinPoint joinPoint, OperateLog operateLog) {
// @OperateLog 注解的情况下 // @OperateLog 注解的情况下
if (operateLog != null) { if (operateLog != null) {

View File

@ -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);
}
}