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

pull/140/head
YunaiV 2024-09-06 21:44:17 +08:00
parent 7889a10846
commit 975ed4ce10
2 changed files with 12 additions and 3 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).getCheckedData(); 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

@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import org.springframework.cloud.openfeign.FeignClient; import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.SpringQueryMap; import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -25,6 +26,16 @@ public interface OperateLogApi {
@Operation(summary = "创建操作日志") @Operation(summary = "创建操作日志")
CommonResult<Boolean> createOperateLog(@Valid @RequestBody OperateLogCreateReqDTO createReqDTO); CommonResult<Boolean> createOperateLog(@Valid @RequestBody OperateLogCreateReqDTO createReqDTO);
/**
*
*
* @param createReqDTO
*/
@Async
default void createOperateLogAsync(OperateLogCreateReqDTO createReqDTO) {
createOperateLog(createReqDTO).checkError();
}
@GetMapping(PREFIX + "/page") @GetMapping(PREFIX + "/page")
@Operation(summary = "获取指定模块的指定数据的操作日志分页") @Operation(summary = "获取指定模块的指定数据的操作日志分页")
CommonResult<PageResult<OperateLogRespDTO>> getOperateLogPage(@SpringQueryMap OperateLogPageReqDTO pageReqDTO); CommonResult<PageResult<OperateLogRespDTO>> getOperateLogPage(@SpringQueryMap OperateLogPageReqDTO pageReqDTO);