From f8a8538b04409a719b55d84960e7da80f06c663c Mon Sep 17 00:00:00 2001 From: "1351515658@qq.com" <1351515658@qq.com> Date: Tue, 30 Jul 2024 18:01:03 +0800 Subject: [PATCH] =?UTF-8?q?optimize=20=E6=93=8D=E4=BD=9C=E6=97=A5=E5=BF=97?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E5=BC=82=E6=AD=A5=E5=86=99=E5=85=A5=E6=97=A5?= =?UTF-8?q?=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/YudaoOperateLogConfiguration.java | 11 +++++-- .../core/service/LogRecordServiceImpl.java | 11 +++---- .../service/OperateLogFrameworkService.java | 19 +++++++++++ .../OperateLogFrameworkServiceImpl.java | 33 +++++++++++++++++++ 4 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkService.java create mode 100644 yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/config/YudaoOperateLogConfiguration.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/config/YudaoOperateLogConfiguration.java index e262c8566..3173263ff 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/config/YudaoOperateLogConfiguration.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/config/YudaoOperateLogConfiguration.java @@ -2,6 +2,8 @@ package cn.iocoder.yudao.framework.operatelog.config; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.operatelog.core.service.LogRecordServiceImpl; +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 com.mzt.logapi.service.ILogRecordService; import com.mzt.logapi.starter.annotation.EnableLogRecord; @@ -22,7 +24,12 @@ public class YudaoOperateLogConfiguration { @Bean @Primary - public ILogRecordService iLogRecordServiceImpl(OperateLogApi operateLogApi) { + public ILogRecordService iLogRecordServiceImpl() { + return new LogRecordServiceImpl(); + } + + @Bean + public OperateLogFrameworkService operateLogFrameworkServiceImpl(OperateLogApi operateLogApi) { // Cloud 专属逻辑:优先使用本地的 operateLogApi 实现类,而不是 Feign 调用 try { OperateLogApi operateLogApiImpl = SpringUtil.getBean("operateLogApiImpl", OperateLogApi.class); @@ -30,7 +37,7 @@ public class YudaoOperateLogConfiguration { operateLogApi = operateLogApiImpl; } } catch (Exception ignored) {} - return new LogRecordServiceImpl(operateLogApi); + return new OperateLogFrameworkServiceImpl(operateLogApi); } } diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/LogRecordServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/LogRecordServiceImpl.java index e782e1544..fea853e7c 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/LogRecordServiceImpl.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/LogRecordServiceImpl.java @@ -4,12 +4,11 @@ import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; -import cn.iocoder.yudao.module.system.api.logger.OperateLogApi; import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO; import com.mzt.logapi.beans.LogRecord; import com.mzt.logapi.service.ILogRecordService; +import jakarta.annotation.Resource; import jakarta.servlet.http.HttpServletRequest; -import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import java.util.List; @@ -17,15 +16,15 @@ import java.util.List; /** * 操作日志 ILogRecordService 实现类 * - * 基于 {@link OperateLogApi} 实现,记录操作日志 + * 基于 {@link OperateLogFrameworkService}实现, 记录操作日志 * * @author HUIHUI */ -@RequiredArgsConstructor @Slf4j public class LogRecordServiceImpl implements ILogRecordService { - private final OperateLogApi operateLogApi; + @Resource + OperateLogFrameworkService operateLogFrameworkService; @Override public void record(LogRecord logRecord) { @@ -40,7 +39,7 @@ public class LogRecordServiceImpl implements ILogRecordService { fillRequestFields(reqDTO); // 2. 异步记录日志 - operateLogApi.createOperateLog(reqDTO); + operateLogFrameworkService.createOperateLog(reqDTO); } private static void fillUserFields(OperateLogCreateReqDTO reqDTO) { diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkService.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkService.java new file mode 100644 index 000000000..cf2cf5b00 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkService.java @@ -0,0 +1,19 @@ +package cn.iocoder.yudao.framework.operatelog.core.service; + +import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO; + +/** + * API 操作日志 Framework Service 接口 + * + * @author 陈晨成 + */ +public interface OperateLogFrameworkService { + + /** + * 创建 操作日志 + * + * @param reqDTO API 操作日志 + */ + void createOperateLog(OperateLogCreateReqDTO reqDTO); + +} diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java new file mode 100644 index 000000000..aa97689ac --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/operatelog/core/service/OperateLogFrameworkServiceImpl.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.framework.operatelog.core.service; + +import cn.iocoder.yudao.module.system.api.logger.OperateLogApi; +import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.annotation.Async; + +/** + * API 操作日志 Framework Service 实现类 + * + * 基于 {@link OperateLogApi} 服务,记录操作日志 + * + * @author 陈晨成 + */ +@RequiredArgsConstructor +@Slf4j +public class OperateLogFrameworkServiceImpl implements OperateLogFrameworkService { + + private final OperateLogApi operateLogApi; + + @Override + @Async + public void createOperateLog(OperateLogCreateReqDTO reqDTO) { + try { + operateLogApi.createOperateLog(reqDTO); + } catch (Throwable ex) { + // 由于 @Async 异步调用,这里打印下日志,更容易跟进 + log.error("[createOperateLog][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex); + } + } + +}