optimize 操作日志组件异步写入日志

pull/130/head
1351515658@qq.com 2024-07-30 18:01:03 +08:00
parent 0431201141
commit f8a8538b04
4 changed files with 66 additions and 8 deletions

View File

@ -2,6 +2,8 @@ package cn.iocoder.yudao.framework.operatelog.config;
import cn.hutool.extra.spring.SpringUtil; import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.yudao.framework.operatelog.core.service.LogRecordServiceImpl; 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 cn.iocoder.yudao.module.system.api.logger.OperateLogApi;
import com.mzt.logapi.service.ILogRecordService; import com.mzt.logapi.service.ILogRecordService;
import com.mzt.logapi.starter.annotation.EnableLogRecord; import com.mzt.logapi.starter.annotation.EnableLogRecord;
@ -22,7 +24,12 @@ public class YudaoOperateLogConfiguration {
@Bean @Bean
@Primary @Primary
public ILogRecordService iLogRecordServiceImpl(OperateLogApi operateLogApi) { public ILogRecordService iLogRecordServiceImpl() {
return new LogRecordServiceImpl();
}
@Bean
public OperateLogFrameworkService operateLogFrameworkServiceImpl(OperateLogApi operateLogApi) {
// Cloud 专属逻辑:优先使用本地的 operateLogApi 实现类,而不是 Feign 调用 // Cloud 专属逻辑:优先使用本地的 operateLogApi 实现类,而不是 Feign 调用
try { try {
OperateLogApi operateLogApiImpl = SpringUtil.getBean("operateLogApiImpl", OperateLogApi.class); OperateLogApi operateLogApiImpl = SpringUtil.getBean("operateLogApiImpl", OperateLogApi.class);
@ -30,7 +37,7 @@ public class YudaoOperateLogConfiguration {
operateLogApi = operateLogApiImpl; operateLogApi = operateLogApiImpl;
} }
} catch (Exception ignored) {} } catch (Exception ignored) {}
return new LogRecordServiceImpl(operateLogApi); return new OperateLogFrameworkServiceImpl(operateLogApi);
} }
} }

View File

@ -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.common.util.servlet.ServletUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; 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 cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO;
import com.mzt.logapi.beans.LogRecord; import com.mzt.logapi.beans.LogRecord;
import com.mzt.logapi.service.ILogRecordService; import com.mzt.logapi.service.ILogRecordService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.List; import java.util.List;
@ -17,15 +16,15 @@ import java.util.List;
/** /**
* ILogRecordService * ILogRecordService
* *
* {@link OperateLogApi} * {@link OperateLogFrameworkService}
* *
* @author HUIHUI * @author HUIHUI
*/ */
@RequiredArgsConstructor
@Slf4j @Slf4j
public class LogRecordServiceImpl implements ILogRecordService { public class LogRecordServiceImpl implements ILogRecordService {
private final OperateLogApi operateLogApi; @Resource
OperateLogFrameworkService operateLogFrameworkService;
@Override @Override
public void record(LogRecord logRecord) { public void record(LogRecord logRecord) {
@ -40,7 +39,7 @@ public class LogRecordServiceImpl implements ILogRecordService {
fillRequestFields(reqDTO); fillRequestFields(reqDTO);
// 2. 异步记录日志 // 2. 异步记录日志
operateLogApi.createOperateLog(reqDTO); operateLogFrameworkService.createOperateLog(reqDTO);
} }
private static void fillUserFields(OperateLogCreateReqDTO reqDTO) { private static void fillUserFields(OperateLogCreateReqDTO reqDTO) {

View File

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

View File

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