【功能修复】框架:操作日志异步记录时,丢失 request 的问题

This commit is contained in:
YunaiV 2024-09-06 21:43:59 +08:00
parent c8ebad91dd
commit 12ce87b305
4 changed files with 13 additions and 6 deletions

View File

@ -11,7 +11,6 @@ import com.mzt.logapi.service.ILogRecordService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import java.util.List; import java.util.List;
@ -29,7 +28,6 @@ public class LogRecordServiceImpl implements ILogRecordService {
private OperateLogApi operateLogApi; private OperateLogApi operateLogApi;
@Override @Override
@Async
public void record(LogRecord logRecord) { public void record(LogRecord logRecord) {
OperateLogCreateReqDTO reqDTO = new OperateLogCreateReqDTO(); OperateLogCreateReqDTO reqDTO = new OperateLogCreateReqDTO();
try { try {
@ -42,7 +40,7 @@ public class LogRecordServiceImpl implements ILogRecordService {
fillRequestFields(reqDTO); fillRequestFields(reqDTO);
// 2. 异步记录日志 // 2. 异步记录日志
operateLogApi.createOperateLog(reqDTO); operateLogApi.createOperateLogAsync(reqDTO);
} catch (Throwable ex) { } catch (Throwable ex) {
// 由于 @Async 异步调用这里打印下日志更容易跟进 // 由于 @Async 异步调用这里打印下日志更容易跟进
log.error("[record][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex); log.error("[record][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex);

View File

@ -92,7 +92,7 @@ public interface CrmReceivableMapper extends BaseMapperX<CrmReceivableDO> {
List<Map<String, Object>> result = selectMaps(new QueryWrapper<CrmReceivableDO>() List<Map<String, Object>> result = selectMaps(new QueryWrapper<CrmReceivableDO>()
.select("contract_id, SUM(price) AS total_price") .select("contract_id, SUM(price) AS total_price")
.in("audit_status", CrmAuditStatusEnum.DRAFT.getStatus(), // 草稿 + 审批中 + 审批通过 .in("audit_status", CrmAuditStatusEnum.DRAFT.getStatus(), // 草稿 + 审批中 + 审批通过
CrmAuditStatusEnum.PROCESS, CrmAuditStatusEnum.APPROVE.getStatus()) CrmAuditStatusEnum.PROCESS.getStatus(), CrmAuditStatusEnum.APPROVE.getStatus())
.groupBy("contract_id") .groupBy("contract_id")
.in("contract_id", contractIds)); .in("contract_id", contractIds));
// 获得金额 // 获得金额

View File

@ -5,6 +5,7 @@ import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogCreateReqDTO;
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogPageReqDTO; import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogPageReqDTO;
import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogRespDTO; import cn.iocoder.yudao.module.system.api.logger.dto.OperateLogRespDTO;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.scheduling.annotation.Async;
/** /**
* 操作日志 API 接口 * 操作日志 API 接口
@ -20,6 +21,16 @@ public interface OperateLogApi {
*/ */
void createOperateLog(@Valid OperateLogCreateReqDTO createReqDTO); void createOperateLog(@Valid OperateLogCreateReqDTO createReqDTO);
/**
* 异步创建操作日志
*
* @param createReqDTO 请求
*/
@Async
default void createOperateLogAsync(OperateLogCreateReqDTO createReqDTO) {
createOperateLog(createReqDTO);
}
/** /**
* 获取指定模块的指定数据的操作日志分页 * 获取指定模块的指定数据的操作日志分页
* *

View File

@ -9,7 +9,6 @@ import cn.iocoder.yudao.module.system.dal.dataobject.logger.OperateLogDO;
import cn.iocoder.yudao.module.system.service.logger.OperateLogService; import cn.iocoder.yudao.module.system.service.logger.OperateLogService;
import com.fhs.core.trans.anno.TransMethodResult; import com.fhs.core.trans.anno.TransMethodResult;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
@ -26,7 +25,6 @@ public class OperateLogApiImpl implements OperateLogApi {
private OperateLogService operateLogService; private OperateLogService operateLogService;
@Override @Override
@Async
public void createOperateLog(OperateLogCreateReqDTO createReqDTO) { public void createOperateLog(OperateLogCreateReqDTO createReqDTO) {
operateLogService.createOperateLog(createReqDTO); operateLogService.createOperateLog(createReqDTO);
} }