mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-24 16:05:08 +08:00
操作日志:完善 code review 提到的问题
This commit is contained in:
@ -47,13 +47,6 @@
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<!-- Spring Boot 通用操作日志组件,基于注解实现 -->
|
||||
<!-- 此组件解决的问题是:「谁」在「什么时间」对「什么」做了「什么事」 -->
|
||||
<groupId>io.github.mouzt</groupId>
|
||||
<artifactId>bizlog-sdk</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
@ -1,19 +1,23 @@
|
||||
package cn.iocoder.yudao.framework.operatelog.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.operatelog.core.aop.OperateLogAspect;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.service.OperateLogFrameworkService;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.service.OperateLogFrameworkServiceImpl;
|
||||
import cn.iocoder.yudao.module.system.api.logger.OperateLogApi;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
@AutoConfiguration
|
||||
public class YudaoOperateLogAutoConfiguration {
|
||||
|
||||
// TODO @puhui999:这个是不是留着哈?因为老版本还是会留着一段时间的
|
||||
//@Bean
|
||||
//public OperateLogAspect operateLogAspect() {
|
||||
// return new OperateLogAspect();
|
||||
//}
|
||||
//
|
||||
//@Bean
|
||||
//public OperateLogFrameworkService operateLogFrameworkService(OperateLogApi operateLogApi) {
|
||||
// return new OperateLogFrameworkServiceImpl(operateLogApi);
|
||||
//}
|
||||
@Bean
|
||||
public OperateLogAspect operateLogAspect() {
|
||||
return new OperateLogAspect();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OperateLogFrameworkService operateLogFrameworkService(OperateLogApi operateLogApi) {
|
||||
return new OperateLogFrameworkServiceImpl(operateLogApi);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,330 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.operatelogv2.core.aop;
|
||||
|
||||
import cn.hutool.core.date.LocalDateTimeUtil;
|
||||
import cn.hutool.core.exceptions.ExceptionUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
||||
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
||||
import cn.iocoder.yudao.module.system.api.logger.OperateLogApi;
|
||||
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogV2CreateReqDTO;
|
||||
import com.google.common.collect.Maps;
|
||||
import com.mzt.logapi.beans.LogRecord;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Array;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.SUCCESS;
|
||||
import static cn.iocoder.yudao.framework.operatelogv2.core.enums.OperateLogV2Constants.*;
|
||||
|
||||
// TODO @puhui999:这个类,是不是可以删除哈;简化简化
|
||||
/**
|
||||
* 拦截使用 @Operation 注解, 获取操作类型、开始时间、持续时间、方法相关信息、执行结果等信息
|
||||
* 对 mzt-biz-log 日志信息进行增强
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Aspect
|
||||
@Slf4j
|
||||
public class OperateLogV2Aspect {
|
||||
|
||||
/**
|
||||
* 用于记录操作内容的上下文
|
||||
*
|
||||
* @see OperateLogV2CreateReqDTO#getContent()
|
||||
*/
|
||||
private static final ThreadLocal<LogRecord> CONTENT = new ThreadLocal<>();
|
||||
/**
|
||||
* 用于记录拓展字段的上下文
|
||||
*
|
||||
* @see OperateLogV2CreateReqDTO#getExtra()
|
||||
*/
|
||||
private static final ThreadLocal<Map<String, Object>> EXTRA = new ThreadLocal<>();
|
||||
|
||||
@Resource
|
||||
private OperateLogApi operateLogApi;
|
||||
|
||||
@Around("@annotation(operation)")
|
||||
public Object around(ProceedingJoinPoint joinPoint, Operation operation) throws Throwable {
|
||||
RequestMethod requestMethod = obtainFirstMatchRequestMethod(obtainRequestMethod(joinPoint));
|
||||
if (requestMethod == RequestMethod.GET) { // 跳过 get 方法
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
// 目前,只有管理员,才记录操作日志!所以非管理员,直接调用,不进行记录
|
||||
Integer userType = WebFrameworkUtils.getLoginUserType();
|
||||
if (ObjUtil.notEqual(userType, UserTypeEnum.ADMIN.getValue())) {
|
||||
return joinPoint.proceed();
|
||||
}
|
||||
|
||||
// 记录开始时间
|
||||
LocalDateTime startTime = LocalDateTime.now();
|
||||
try {
|
||||
// 执行原有方法
|
||||
Object result = joinPoint.proceed();
|
||||
// 记录正常执行时的操作日志
|
||||
this.log(joinPoint, operation, startTime, result, null);
|
||||
return result;
|
||||
} catch (Throwable exception) {
|
||||
this.log(joinPoint, operation, startTime, null, exception);
|
||||
throw exception;
|
||||
} finally {
|
||||
clearThreadLocal();
|
||||
}
|
||||
}
|
||||
|
||||
public static void setContent(LogRecord content) {
|
||||
CONTENT.set(content);
|
||||
}
|
||||
|
||||
public static void addExtra(String key, Object value) {
|
||||
if (EXTRA.get() == null) {
|
||||
EXTRA.set(new HashMap<>());
|
||||
}
|
||||
EXTRA.get().put(key, value);
|
||||
}
|
||||
|
||||
public static void addExtra(Map<String, Object> extra) {
|
||||
if (EXTRA.get() == null) {
|
||||
EXTRA.set(new HashMap<>());
|
||||
}
|
||||
EXTRA.get().putAll(extra);
|
||||
}
|
||||
|
||||
private static void clearThreadLocal() {
|
||||
CONTENT.remove();
|
||||
EXTRA.remove();
|
||||
}
|
||||
|
||||
private void log(ProceedingJoinPoint joinPoint, Operation operation,
|
||||
LocalDateTime startTime, Object result, Throwable exception) {
|
||||
try {
|
||||
// 判断不记录的情况(默认没有值就是记录)
|
||||
if (EXTRA.get() != null && EXTRA.get().get(ENABLE) != null) {
|
||||
return;
|
||||
}
|
||||
if (CONTENT.get() == null) { // 没有值说明没有日志需要记录
|
||||
return;
|
||||
}
|
||||
|
||||
// 真正记录操作日志
|
||||
this.log0(joinPoint, operation, startTime, result, exception);
|
||||
} catch (Throwable ex) {
|
||||
log.error("[log][记录操作日志时,发生异常,其中参数是 joinPoint({}) apiOperation({}) result({}) exception({}) ]",
|
||||
joinPoint, operation, result, exception, ex);
|
||||
}
|
||||
}
|
||||
|
||||
private void log0(ProceedingJoinPoint joinPoint, Operation operation,
|
||||
LocalDateTime startTime, Object result, Throwable exception) {
|
||||
OperateLogV2CreateReqDTO reqDTO = new OperateLogV2CreateReqDTO();
|
||||
// 补全通用字段
|
||||
reqDTO.setTraceId(TracerUtils.getTraceId());
|
||||
reqDTO.setStartTime(startTime);
|
||||
// 补充用户信息
|
||||
fillUserFields(reqDTO);
|
||||
// 补全模块信息
|
||||
fillModuleFields(reqDTO, operation);
|
||||
// 补全请求信息
|
||||
fillRequestFields(reqDTO);
|
||||
// 补全方法信息
|
||||
fillMethodFields(reqDTO, joinPoint, startTime, result, exception);
|
||||
|
||||
// 异步记录日志
|
||||
operateLogApi.createOperateLogV2(reqDTO);
|
||||
}
|
||||
|
||||
private static void fillUserFields(OperateLogV2CreateReqDTO reqDTO) {
|
||||
reqDTO.setUserId(WebFrameworkUtils.getLoginUserId());
|
||||
reqDTO.setUserType(WebFrameworkUtils.getLoginUserType());
|
||||
}
|
||||
|
||||
private static void fillModuleFields(OperateLogV2CreateReqDTO reqDTO, Operation operation) {
|
||||
LogRecord logRecord = CONTENT.get();
|
||||
reqDTO.setBizId(Long.parseLong(logRecord.getBizNo())); // 操作模块业务编号
|
||||
reqDTO.setContent(logRecord.getAction());// 例如说,修改编号为 1 的用户信息,将性别从男改成女,将姓名从芋道改成源码。
|
||||
|
||||
// type 属性
|
||||
reqDTO.setType(logRecord.getType()); // 大模块类型如 crm 客户
|
||||
// subType 属性
|
||||
if (logRecord.getSubType() != null) {
|
||||
reqDTO.setSubType(logRecord.getSubType());// 操作名称如 转移客户
|
||||
}
|
||||
if (StrUtil.isEmpty(reqDTO.getSubType()) && operation != null) {
|
||||
reqDTO.setSubType(operation.summary());
|
||||
}
|
||||
|
||||
// 拓展字段,有些复杂的业务,需要记录一些字段 ( JSON 格式 ),例如说,记录订单编号,{ orderId: "1"}
|
||||
Map<String, Object> objectMap = EXTRA.get();
|
||||
if (objectMap != null) {
|
||||
Object object = objectMap.get(EXTRA_KEY);
|
||||
if (object instanceof Map<?, ?> extraMap) {
|
||||
if (extraMap.keySet().stream().allMatch(String.class::isInstance)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> extra = (Map<String, Object>) extraMap;
|
||||
reqDTO.setExtra(extra);
|
||||
return;
|
||||
}
|
||||
}
|
||||
// 激进一点不是 map 直接当 value 处理
|
||||
Map<String, Object> extra = Maps.newHashMapWithExpectedSize(1);
|
||||
extra.put(EXTRA_KEY, object);
|
||||
reqDTO.setExtra(extra);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static void fillRequestFields(OperateLogV2CreateReqDTO reqDTO) {
|
||||
// 获得 Request 对象
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
if (request == null) {
|
||||
return;
|
||||
}
|
||||
// 补全请求信息
|
||||
reqDTO.setRequestMethod(request.getMethod());
|
||||
reqDTO.setRequestUrl(request.getRequestURI());
|
||||
reqDTO.setUserIp(ServletUtils.getClientIP(request));
|
||||
reqDTO.setUserAgent(ServletUtils.getUserAgent(request));
|
||||
}
|
||||
|
||||
private static void fillMethodFields(OperateLogV2CreateReqDTO reqDTO, ProceedingJoinPoint joinPoint, LocalDateTime startTime,
|
||||
Object result, Throwable exception) {
|
||||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
||||
reqDTO.setJavaMethod(methodSignature.toString());
|
||||
if (EXTRA.get().get(IS_LOG_ARGS) == null) {
|
||||
reqDTO.setJavaMethodArgs(obtainMethodArgs(joinPoint));
|
||||
}
|
||||
if (EXTRA.get().get(IS_LOG_RESULT_DATA) == null) {
|
||||
reqDTO.setResultData(obtainResultData(result));
|
||||
}
|
||||
reqDTO.setDuration((int) (LocalDateTimeUtil.between(startTime, LocalDateTime.now()).toMillis()));
|
||||
// (正常)处理 resultCode 和 resultMsg 字段
|
||||
if (result instanceof CommonResult) {
|
||||
CommonResult<?> commonResult = (CommonResult<?>) result;
|
||||
reqDTO.setResultCode(commonResult.getCode());
|
||||
reqDTO.setResultMsg(commonResult.getMsg());
|
||||
} else {
|
||||
reqDTO.setResultCode(SUCCESS.getCode());
|
||||
}
|
||||
// (异常)处理 resultCode 和 resultMsg 字段
|
||||
if (exception != null) {
|
||||
reqDTO.setResultCode(INTERNAL_SERVER_ERROR.getCode());
|
||||
reqDTO.setResultMsg(ExceptionUtil.getRootCauseMessage(exception));
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
private static <T extends Annotation> T getClassAnnotation(ProceedingJoinPoint joinPoint, Class<T> annotationClass) {
|
||||
return ((MethodSignature) joinPoint.getSignature()).getMethod().getDeclaringClass().getAnnotation(annotationClass);
|
||||
}
|
||||
|
||||
private static String obtainMethodArgs(ProceedingJoinPoint joinPoint) {
|
||||
// TODO 提升:参数脱敏和忽略
|
||||
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
|
||||
String[] argNames = methodSignature.getParameterNames();
|
||||
Object[] argValues = joinPoint.getArgs();
|
||||
// 拼接参数
|
||||
Map<String, Object> args = Maps.newHashMapWithExpectedSize(argValues.length);
|
||||
for (int i = 0; i < argNames.length; i++) {
|
||||
String argName = argNames[i];
|
||||
Object argValue = argValues[i];
|
||||
// 被忽略时,标记为 ignore 字符串,避免和 null 混在一起
|
||||
args.put(argName, !isIgnoreArgs(argValue) ? argValue : "[ignore]");
|
||||
}
|
||||
return JsonUtils.toJsonString(args);
|
||||
}
|
||||
|
||||
private static String obtainResultData(Object result) {
|
||||
// TODO 提升:结果脱敏和忽略
|
||||
if (result instanceof CommonResult) {
|
||||
result = ((CommonResult<?>) result).getData();
|
||||
}
|
||||
return JsonUtils.toJsonString(result);
|
||||
}
|
||||
|
||||
private static boolean isIgnoreArgs(Object object) {
|
||||
Class<?> clazz = object.getClass();
|
||||
// 处理数组的情况
|
||||
if (clazz.isArray()) {
|
||||
return IntStream.range(0, Array.getLength(object))
|
||||
.anyMatch(index -> isIgnoreArgs(Array.get(object, index)));
|
||||
}
|
||||
// 递归,处理数组、Collection、Map 的情况
|
||||
if (Collection.class.isAssignableFrom(clazz)) {
|
||||
return ((Collection<?>) object).stream()
|
||||
.anyMatch((Predicate<Object>) OperateLogV2Aspect::isIgnoreArgs);
|
||||
}
|
||||
if (Map.class.isAssignableFrom(clazz)) {
|
||||
return isIgnoreArgs(((Map<?, ?>) object).values());
|
||||
}
|
||||
// obj
|
||||
return object instanceof MultipartFile
|
||||
|| object instanceof HttpServletRequest
|
||||
|| object instanceof HttpServletResponse
|
||||
|| object instanceof BindingResult;
|
||||
}
|
||||
|
||||
private static RequestMethod obtainFirstMatchRequestMethod(RequestMethod[] requestMethods) {
|
||||
if (ArrayUtil.isEmpty(requestMethods)) {
|
||||
return null;
|
||||
}
|
||||
// 优先,匹配最优的 POST、PUT、DELETE
|
||||
RequestMethod result = obtainFirstLogRequestMethod(requestMethods);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
// 然后,匹配次优的 GET
|
||||
result = Arrays.stream(requestMethods).filter(requestMethod -> requestMethod == RequestMethod.GET)
|
||||
.findFirst().orElse(null);
|
||||
if (result != null) {
|
||||
return result;
|
||||
}
|
||||
// 兜底,获得第一个
|
||||
return requestMethods[0];
|
||||
}
|
||||
|
||||
private static RequestMethod[] obtainRequestMethod(ProceedingJoinPoint joinPoint) {
|
||||
RequestMapping requestMapping = AnnotationUtils.getAnnotation( // 使用 Spring 的工具类,可以处理 @RequestMapping 别名注解
|
||||
((MethodSignature) joinPoint.getSignature()).getMethod(), RequestMapping.class);
|
||||
return requestMapping != null ? requestMapping.method() : new RequestMethod[]{};
|
||||
}
|
||||
|
||||
private static RequestMethod obtainFirstLogRequestMethod(RequestMethod[] requestMethods) {
|
||||
if (ArrayUtil.isEmpty(requestMethods)) {
|
||||
return null;
|
||||
}
|
||||
return Arrays.stream(requestMethods).filter(requestMethod ->
|
||||
requestMethod == RequestMethod.POST
|
||||
|| requestMethod == RequestMethod.PUT
|
||||
|| requestMethod == RequestMethod.DELETE)
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
|
||||
}
|
@ -1,35 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.operatelogv2.core.enums;
|
||||
|
||||
// TODO @puhui999:这个类,是不是可以删除哈;
|
||||
/**
|
||||
* 操作日志常量接口
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
public interface OperateLogV2Constants {
|
||||
|
||||
// ========== 开关字段-如果没有值默认就是记录 ==========
|
||||
|
||||
/**
|
||||
* 是否记录日志
|
||||
*/
|
||||
String ENABLE = "enable";
|
||||
|
||||
/**
|
||||
* 是否记录方法参数
|
||||
*/
|
||||
String IS_LOG_ARGS = "isLogArgs";
|
||||
|
||||
/**
|
||||
* 是否记录方法结果的数据
|
||||
*/
|
||||
String IS_LOG_RESULT_DATA = "isLogResultData";
|
||||
|
||||
// ========== 扩展 ==========
|
||||
|
||||
/**
|
||||
* 扩展信息
|
||||
*/
|
||||
String EXTRA_KEY = "extra";
|
||||
|
||||
}
|
@ -1,39 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.operatelogv2.core.service;
|
||||
|
||||
import cn.iocoder.yudao.framework.operatelogv2.core.aop.OperateLogV2Aspect;
|
||||
import com.mzt.logapi.beans.LogRecord;
|
||||
import com.mzt.logapi.context.LogRecordContext;
|
||||
import com.mzt.logapi.service.ILogRecordService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志 ILogRecordService 实现类
|
||||
*
|
||||
* 基于 {@link OperateLogV2Aspect} 实现,记录操作日志
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Slf4j
|
||||
public class ILogRecordServiceImpl implements ILogRecordService {
|
||||
|
||||
@Override
|
||||
public void record(LogRecord logRecord) {
|
||||
OperateLogV2Aspect.setContent(logRecord); // 操作日志
|
||||
OperateLogV2Aspect.addExtra(LogRecordContext.getVariables()); // 扩展信息
|
||||
// TODO @puhui999:这里是不是调用 operateLogApi 进行记录哈
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogRecord> queryLog(String bizNo, String type) {
|
||||
throw new UnsupportedOperationException("不支持该操作,请使用 OperateLogApi 查询!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogRecord> queryLogByBizNo(String bizNo, String type, String subType) {
|
||||
throw new UnsupportedOperationException("不支持该操作,请使用 OperateLogApi 查询!");
|
||||
}
|
||||
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.operatelogv2.core.vo;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageParam;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
// TODO @puhui999:这个最好每个模块自己弄哈,放这里有点怪可能
|
||||
@Schema(description = "管理后台 - 操作日志分页 Request VO")
|
||||
@Data
|
||||
public class OperateLogV2PageReqVO extends PageParam {
|
||||
|
||||
@Schema(description = "模块数据编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long bizId;
|
||||
|
||||
@Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "模块类型", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
|
||||
private String bizType;
|
||||
|
||||
}
|
@ -1 +0,0 @@
|
||||
package cn.iocoder.yudao.framework.operatelogv2;
|
@ -1,2 +1 @@
|
||||
cn.iocoder.yudao.framework.operatelog.config.YudaoOperateLogAutoConfiguration
|
||||
cn.iocoder.yudao.framework.operatelogv2.config.YudaoOperateLogV2Configuration
|
||||
cn.iocoder.yudao.framework.operatelog.config.YudaoOperateLogAutoConfiguration
|
@ -50,6 +50,13 @@
|
||||
<artifactId>guava</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<!-- Spring Boot 通用操作日志组件,基于注解实现 -->
|
||||
<!-- 此组件解决的问题是:「谁」在「什么时间」对「什么」做了「什么事」 -->
|
||||
<groupId>io.github.mouzt</groupId>
|
||||
<artifactId>bizlog-sdk</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 业务组件 -->
|
||||
<dependency>
|
||||
<groupId>cn.iocoder.boot</groupId>
|
||||
|
@ -1,7 +1,6 @@
|
||||
package cn.iocoder.yudao.framework.operatelogv2.config;
|
||||
package cn.iocoder.yudao.framework.operatelog.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.operatelogv2.core.aop.OperateLogV2Aspect;
|
||||
import cn.iocoder.yudao.framework.operatelogv2.core.service.ILogRecordServiceImpl;
|
||||
import cn.iocoder.yudao.framework.operatelog.core.service.ILogRecordServiceImpl;
|
||||
import com.mzt.logapi.service.ILogRecordService;
|
||||
import com.mzt.logapi.starter.annotation.EnableLogRecord;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -9,7 +8,7 @@ import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
// TODO @puhui999:思考了下,为了减少 starter 数量,在 security 组件里,增加一个 core/operatelog 包,然后 YudaoOperateLogV2Configuration 搞一个过去;
|
||||
|
||||
/**
|
||||
* mzt-biz-log 配置类
|
||||
*
|
||||
@ -26,9 +25,4 @@ public class YudaoOperateLogV2Configuration {
|
||||
return new ILogRecordServiceImpl();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public OperateLogV2Aspect operateLogV2Aspect() {
|
||||
return new OperateLogV2Aspect();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
package cn.iocoder.yudao.framework.operatelog.core;
|
@ -0,0 +1,83 @@
|
||||
package cn.iocoder.yudao.framework.operatelog.core.service;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
|
||||
import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
|
||||
import cn.iocoder.yudao.module.system.api.logger.OperateLogApi;
|
||||
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogV2CreateReqDTO;
|
||||
import com.mzt.logapi.beans.LogRecord;
|
||||
import com.mzt.logapi.service.ILogRecordService;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 操作日志 ILogRecordService 实现类
|
||||
*
|
||||
* 基于 {@link OperateLogApi} 实现,记录操作日志
|
||||
*
|
||||
* @author HUIHUI
|
||||
*/
|
||||
@Slf4j
|
||||
public class ILogRecordServiceImpl implements ILogRecordService {
|
||||
|
||||
@Resource
|
||||
private OperateLogApi operateLogApi;
|
||||
|
||||
@Override
|
||||
public void record(LogRecord logRecord) {
|
||||
OperateLogV2CreateReqDTO reqDTO = new OperateLogV2CreateReqDTO();
|
||||
// 补全通用字段
|
||||
reqDTO.setTraceId(TracerUtils.getTraceId());
|
||||
// 补充用户信息
|
||||
fillUserFields(reqDTO);
|
||||
// 补全模块信息
|
||||
fillModuleFields(reqDTO, logRecord);
|
||||
// 补全请求信息
|
||||
fillRequestFields(reqDTO);
|
||||
// 异步记录日志
|
||||
operateLogApi.createOperateLogV2(reqDTO);
|
||||
// TODO 测试结束删除或搞个开关
|
||||
log.info("操作日志 ===> {}", reqDTO);
|
||||
}
|
||||
|
||||
private static void fillUserFields(OperateLogV2CreateReqDTO reqDTO) {
|
||||
reqDTO.setUserId(WebFrameworkUtils.getLoginUserId());
|
||||
reqDTO.setUserType(WebFrameworkUtils.getLoginUserType());
|
||||
}
|
||||
|
||||
public static void fillModuleFields(OperateLogV2CreateReqDTO reqDTO, LogRecord logRecord) {
|
||||
reqDTO.setType(logRecord.getType()); // 大模块类型如 crm 客户
|
||||
reqDTO.setSubType(logRecord.getSubType());// 操作名称如 转移客户
|
||||
reqDTO.setBizId(Long.parseLong(logRecord.getBizNo())); // 操作模块业务编号
|
||||
reqDTO.setAction(logRecord.getAction());// 例如说,修改编号为 1 的用户信息,将性别从男改成女,将姓名从芋道改成源码。
|
||||
reqDTO.setExtra(logRecord.getExtra()); // 拓展字段,有些复杂的业务,需要记录一些字段 ( JSON 格式 ),例如说,记录订单编号,{ orderId: "1"}
|
||||
}
|
||||
|
||||
private static void fillRequestFields(OperateLogV2CreateReqDTO reqDTO) {
|
||||
// 获得 Request 对象
|
||||
HttpServletRequest request = ServletUtils.getRequest();
|
||||
if (request == null) {
|
||||
return;
|
||||
}
|
||||
// 补全请求信息
|
||||
reqDTO.setRequestMethod(request.getMethod());
|
||||
reqDTO.setRequestUrl(request.getRequestURI());
|
||||
reqDTO.setUserIp(ServletUtils.getClientIP(request));
|
||||
reqDTO.setUserAgent(ServletUtils.getUserAgent(request));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogRecord> queryLog(String bizNo, String type) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<LogRecord> queryLogByBizNo(String bizNo, String type, String subType) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
}
|
@ -1,2 +1,3 @@
|
||||
cn.iocoder.yudao.framework.security.config.YudaoSecurityAutoConfiguration
|
||||
cn.iocoder.yudao.framework.security.config.YudaoWebSecurityConfigurerAdapter
|
||||
cn.iocoder.yudao.framework.security.config.YudaoWebSecurityConfigurerAdapter
|
||||
cn.iocoder.yudao.framework.operatelog.config.YudaoOperateLogV2Configuration
|
Reference in New Issue
Block a user