完善操作日志

pull/2/head
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);
}
}