optimize 操作日志组件异步写入日志
parent
0431201141
commit
f8a8538b04
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
||||||
|
|
|
@ -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);
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue