Commit 2f9d9723b3f5e8143a1d3570657a79bc4376b0f7

Authored by YunaiV
1 parent 12ce87b3

【代码优化】框架:简化 api 访问日志、错误日志的记录逻辑

yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/config/YudaoApiLogAutoConfiguration.java
... ... @@ -2,15 +2,10 @@ package cn.iocoder.yudao.framework.apilog.config;
2 2  
3 3 import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter;
4 4 import cn.iocoder.yudao.framework.apilog.core.interceptor.ApiAccessLogInterceptor;
5   -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
6   -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl;
7   -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
8   -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl;
9 5 import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
10 6 import cn.iocoder.yudao.framework.web.config.WebProperties;
11 7 import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration;
12 8 import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi;
13   -import cn.iocoder.yudao.module.infra.api.logger.ApiErrorLogApi;
14 9 import jakarta.servlet.Filter;
15 10 import org.springframework.beans.factory.annotation.Value;
16 11 import org.springframework.boot.autoconfigure.AutoConfiguration;
... ... @@ -23,18 +18,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
23 18 @AutoConfiguration(after = YudaoWebAutoConfiguration.class)
24 19 public class YudaoApiLogAutoConfiguration implements WebMvcConfigurer {
25 20  
26   - @Bean
27   - @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
28   - public ApiAccessLogFrameworkService apiAccessLogFrameworkService(ApiAccessLogApi apiAccessLogApi) {
29   - return new ApiAccessLogFrameworkServiceImpl(apiAccessLogApi);
30   - }
31   -
32   - @Bean
33   - @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
34   - public ApiErrorLogFrameworkService apiErrorLogFrameworkService(ApiErrorLogApi apiErrorLogApi) {
35   - return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi);
36   - }
37   -
38 21 /**
39 22 * 创建 ApiAccessLogFilter Bean,记录 API 请求日志
40 23 */
... ... @@ -42,8 +25,8 @@ public class YudaoApiLogAutoConfiguration implements WebMvcConfigurer {
42 25 @ConditionalOnProperty(prefix = "yudao.access-log", value = "enable", matchIfMissing = true) // 允许使用 yudao.access-log.enable=false 禁用访问日志
43 26 public FilterRegistrationBean<ApiAccessLogFilter> apiAccessLogFilter(WebProperties webProperties,
44 27 @Value("${spring.application.name}") String applicationName,
45   - ApiAccessLogFrameworkService apiAccessLogFrameworkService) {
46   - ApiAccessLogFilter filter = new ApiAccessLogFilter(webProperties, applicationName, apiAccessLogFrameworkService);
  28 + ApiAccessLogApi apiAccessLogApi) {
  29 + ApiAccessLogFilter filter = new ApiAccessLogFilter(webProperties, applicationName, apiAccessLogApi);
47 30 return createFilterBean(filter, WebFilterOrderEnum.API_ACCESS_LOG_FILTER);
48 31 }
49 32  
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/filter/ApiAccessLogFilter.java
... ... @@ -9,7 +9,6 @@ import cn.hutool.core.util.BooleanUtil;
9 9 import cn.hutool.core.util.StrUtil;
10 10 import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
11 11 import cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum;
12   -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService;
13 12 import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants;
14 13 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
15 14 import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
... ... @@ -18,6 +17,7 @@ import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
18 17 import cn.iocoder.yudao.framework.web.config.WebProperties;
19 18 import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter;
20 19 import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
  20 +import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi;
21 21 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
22 22 import com.fasterxml.jackson.databind.JsonNode;
23 23 import io.swagger.v3.oas.annotations.Operation;
... ... @@ -36,7 +36,7 @@ import java.time.temporal.ChronoUnit;
36 36 import java.util.Iterator;
37 37 import java.util.Map;
38 38  
39   -import static cn.iocoder.yudao.framework.apilog.core.interceptor.ApiAccessLogInterceptor.*;
  39 +import static cn.iocoder.yudao.framework.apilog.core.interceptor.ApiAccessLogInterceptor.ATTRIBUTE_HANDLER_METHOD;
40 40 import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
41 41  
42 42 /**
... ... @@ -53,12 +53,12 @@ public class ApiAccessLogFilter extends ApiRequestFilter {
53 53  
54 54 private final String applicationName;
55 55  
56   - private final ApiAccessLogFrameworkService apiAccessLogFrameworkService;
  56 + private final ApiAccessLogApi apiAccessLogApi;
57 57  
58   - public ApiAccessLogFilter(WebProperties webProperties, String applicationName, ApiAccessLogFrameworkService apiAccessLogFrameworkService) {
  58 + public ApiAccessLogFilter(WebProperties webProperties, String applicationName, ApiAccessLogApi apiAccessLogApi) {
59 59 super(webProperties);
60 60 this.applicationName = applicationName;
61   - this.apiAccessLogFrameworkService = apiAccessLogFrameworkService;
  61 + this.apiAccessLogApi = apiAccessLogApi;
62 62 }
63 63  
64 64 @Override
... ... @@ -91,7 +91,7 @@ public class ApiAccessLogFilter extends ApiRequestFilter {
91 91 if (!enable) {
92 92 return;
93 93 }
94   - apiAccessLogFrameworkService.createApiAccessLog(accessLog);
  94 + apiAccessLogApi.createApiAccessLogAsync(accessLog);
95 95 } catch (Throwable th) {
96 96 log.error("[createApiAccessLog][url({}) log({}) 发生异常]", request.getRequestURI(), toJsonString(accessLog), th);
97 97 }
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkService.java deleted
1   -package cn.iocoder.yudao.framework.apilog.core.service;
2   -
3   -import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
4   -
5   -/**
6   - * API 访问日志 Framework Service 接口
7   - *
8   - * @author 芋道源码
9   - */
10   -public interface ApiAccessLogFrameworkService {
11   -
12   - /**
13   - * 创建 API 访问日志
14   - *
15   - * @param reqDTO API 访问日志
16   - */
17   - void createApiAccessLog(ApiAccessLogCreateReqDTO reqDTO);
18   -
19   -}
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java deleted
1   -package cn.iocoder.yudao.framework.apilog.core.service;
2   -
3   -import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi;
4   -import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
5   -import lombok.RequiredArgsConstructor;
6   -import lombok.extern.slf4j.Slf4j;
7   -import org.springframework.scheduling.annotation.Async;
8   -
9   -/**
10   - * API 访问日志 Framework Service 实现类
11   - *
12   - * 基于 {@link ApiAccessLogApi} 服务,记录访问日志
13   - *
14   - * @author 芋道源码
15   - */
16   -@RequiredArgsConstructor
17   -@Slf4j
18   -public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkService {
19   -
20   - private final ApiAccessLogApi apiAccessLogApi;
21   -
22   - @Override
23   - @Async
24   - public void createApiAccessLog(ApiAccessLogCreateReqDTO reqDTO) {
25   - try {
26   - apiAccessLogApi.createApiAccessLog(reqDTO);
27   - } catch (Throwable ex) {
28   - // 由于 @Async 异步调用,这里打印下日志,更容易跟进
29   - log.error("[createApiAccessLog][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex);
30   - }
31   - }
32   -
33   -}
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkService.java deleted
1   -package cn.iocoder.yudao.framework.apilog.core.service;
2   -
3   -import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
4   -
5   -/**
6   - * API 错误日志 Framework Service 接口
7   - *
8   - * @author 芋道源码
9   - */
10   -public interface ApiErrorLogFrameworkService {
11   -
12   - /**
13   - * 创建 API 错误日志
14   - *
15   - * @param reqDTO API 错误日志
16   - */
17   - void createApiErrorLog(ApiErrorLogCreateReqDTO reqDTO);
18   -
19   -}
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java deleted
1   -package cn.iocoder.yudao.framework.apilog.core.service;
2   -
3   -import cn.iocoder.yudao.module.infra.api.logger.ApiErrorLogApi;
4   -import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
5   -import lombok.RequiredArgsConstructor;
6   -import lombok.extern.slf4j.Slf4j;
7   -import org.springframework.scheduling.annotation.Async;
8   -
9   -/**
10   - * API 错误日志 Framework Service 实现类
11   - *
12   - * 基于 {@link ApiErrorLogApi} 服务,记录错误日志
13   - *
14   - * @author 芋道源码
15   - */
16   -@RequiredArgsConstructor
17   -@Slf4j
18   -public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkService {
19   -
20   - private final ApiErrorLogApi apiErrorLogApi;
21   -
22   - @Override
23   - @Async
24   - public void createApiErrorLog(ApiErrorLogCreateReqDTO reqDTO) {
25   - try {
26   - apiErrorLogApi.createApiErrorLog(reqDTO);
27   - } catch (Throwable ex) {
28   - // 由于 @Async 异步调用,这里打印下日志,更容易跟进
29   - log.error("[createApiErrorLog][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex);
30   - }
31   - }
32   -
33   -}
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/config/YudaoWebAutoConfiguration.java
1 1 package cn.iocoder.yudao.framework.web.config;
2 2  
3   -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
4 3 import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
5 4 import cn.iocoder.yudao.framework.web.core.filter.CacheRequestBodyFilter;
6 5 import cn.iocoder.yudao.framework.web.core.filter.DemoFilter;
7 6 import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
8 7 import cn.iocoder.yudao.framework.web.core.handler.GlobalResponseBodyHandler;
9 8 import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
  9 +import cn.iocoder.yudao.module.infra.api.logger.ApiErrorLogApi;
  10 +import jakarta.annotation.Resource;
  11 +import jakarta.servlet.Filter;
10 12 import org.springframework.beans.factory.annotation.Value;
11 13 import org.springframework.boot.autoconfigure.AutoConfiguration;
12 14 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
... ... @@ -25,9 +27,6 @@ import org.springframework.web.filter.CorsFilter;
25 27 import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
26 28 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
27 29  
28   -import jakarta.annotation.Resource;
29   -import jakarta.servlet.Filter;
30   -
31 30 @AutoConfiguration
32 31 @EnableConfigurationProperties(WebProperties.class)
33 32 public class YudaoWebAutoConfiguration implements WebMvcConfigurer {
... ... @@ -59,8 +58,9 @@ public class YudaoWebAutoConfiguration implements WebMvcConfigurer {
59 58 }
60 59  
61 60 @Bean
62   - public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogFrameworkService apiErrorLogFrameworkService) {
63   - return new GlobalExceptionHandler(applicationName, apiErrorLogFrameworkService);
  61 + @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
  62 + public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogApi apiErrorLogApi) {
  63 + return new GlobalExceptionHandler(applicationName, apiErrorLogApi);
64 64 }
65 65  
66 66 @Bean
... ...
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/handler/GlobalExceptionHandler.java
... ... @@ -5,7 +5,6 @@ import cn.hutool.core.map.MapUtil;
5 5 import cn.hutool.core.util.ObjUtil;
6 6 import cn.hutool.core.util.StrUtil;
7 7 import cn.hutool.extra.servlet.JakartaServletUtil;
8   -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService;
9 8 import cn.iocoder.yudao.framework.common.exception.ServiceException;
10 9 import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
11 10 import cn.iocoder.yudao.framework.common.pojo.CommonResult;
... ... @@ -14,6 +13,7 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
14 13 import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils;
15 14 import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils;
16 15 import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils;
  16 +import cn.iocoder.yudao.module.infra.api.logger.ApiErrorLogApi;
17 17 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
18 18 import com.fasterxml.jackson.databind.exc.InvalidFormatException;
19 19 import jakarta.servlet.http.HttpServletRequest;
... ... @@ -40,12 +40,7 @@ import java.time.LocalDateTime;
40 40 import java.util.Map;
41 41 import java.util.Set;
42 42  
43   -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.BAD_REQUEST;
44   -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.FORBIDDEN;
45   -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
46   -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.METHOD_NOT_ALLOWED;
47   -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;
48   -import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_IMPLEMENTED;
  43 +import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.*;
49 44  
50 45 /**
51 46 * 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
... ... @@ -65,7 +60,7 @@ public class GlobalExceptionHandler {
65 60 @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
66 61 private final String applicationName;
67 62  
68   - private final ApiErrorLogFrameworkService apiErrorLogFrameworkService;
  63 + private final ApiErrorLogApi apiErrorLogApi;
69 64  
70 65 /**
71 66 * 处理所有异常,主要是提供给 Filter 使用
... ... @@ -288,7 +283,7 @@ public class GlobalExceptionHandler {
288 283 // 初始化 errorLog
289 284 buildExceptionLog(errorLog, req, e);
290 285 // 执行插入 errorLog
291   - apiErrorLogFrameworkService.createApiErrorLog(errorLog);
  286 + apiErrorLogApi.createApiErrorLogAsync(errorLog);
292 287 } catch (Throwable th) {
293 288 log.error("[createExceptionLog][url({}) log({}) 发生异常]", req.getRequestURI(), JsonUtils.toJsonString(errorLog), th);
294 289 }
... ...
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiAccessLogApi.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.infra.api.logger;
3 3 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO;
4 4  
5 5 import jakarta.validation.Valid;
  6 +import org.springframework.scheduling.annotation.Async;
6 7  
7 8 /**
8 9 * API 访问日志的 API 接口
... ... @@ -18,4 +19,14 @@ public interface ApiAccessLogApi {
18 19 */
19 20 void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO);
20 21  
  22 + /**
  23 + * 【异步】创建 API 访问日志
  24 + *
  25 + * @param createDTO 访问日志 DTO
  26 + */
  27 + @Async
  28 + default void createApiAccessLogAsync(ApiAccessLogCreateReqDTO createDTO) {
  29 + createApiAccessLog(createDTO);
  30 + }
  31 +
21 32 }
... ...
yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/api/logger/ApiErrorLogApi.java
... ... @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.infra.api.logger;
3 3 import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO;
4 4  
5 5 import jakarta.validation.Valid;
  6 +import org.springframework.scheduling.annotation.Async;
6 7  
7 8 /**
8 9 * API 错误日志的 API 接口
... ... @@ -18,4 +19,14 @@ public interface ApiErrorLogApi {
18 19 */
19 20 void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO);
20 21  
  22 + /**
  23 + * 【异步】创建 API 异常日志
  24 + *
  25 + * @param createDTO 异常日志 DTO
  26 + */
  27 + @Async
  28 + default void createApiErrorLogAsync(ApiErrorLogCreateReqDTO createDTO) {
  29 + createApiErrorLog(createDTO);
  30 + }
  31 +
21 32 }
... ...