Commit 2f9d9723b3f5e8143a1d3570657a79bc4376b0f7
1 parent
12ce87b3
【代码优化】框架:简化 api 访问日志、错误日志的记录逻辑
Showing
10 changed files
with
40 additions
and
144 deletions
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,15 +2,10 @@ package cn.iocoder.yudao.framework.apilog.config; | ||
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter; | 3 | import cn.iocoder.yudao.framework.apilog.core.filter.ApiAccessLogFilter; |
| 4 | import cn.iocoder.yudao.framework.apilog.core.interceptor.ApiAccessLogInterceptor; | 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 | import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; |
| 10 | import cn.iocoder.yudao.framework.web.config.WebProperties; | 6 | import cn.iocoder.yudao.framework.web.config.WebProperties; |
| 11 | import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration; | 7 | import cn.iocoder.yudao.framework.web.config.YudaoWebAutoConfiguration; |
| 12 | import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi; | 8 | import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi; |
| 13 | -import cn.iocoder.yudao.module.infra.api.logger.ApiErrorLogApi; | ||
| 14 | import jakarta.servlet.Filter; | 9 | import jakarta.servlet.Filter; |
| 15 | import org.springframework.beans.factory.annotation.Value; | 10 | import org.springframework.beans.factory.annotation.Value; |
| 16 | import org.springframework.boot.autoconfigure.AutoConfiguration; | 11 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| @@ -23,18 +18,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | @@ -23,18 +18,6 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
| 23 | @AutoConfiguration(after = YudaoWebAutoConfiguration.class) | 18 | @AutoConfiguration(after = YudaoWebAutoConfiguration.class) |
| 24 | public class YudaoApiLogAutoConfiguration implements WebMvcConfigurer { | 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 | * 创建 ApiAccessLogFilter Bean,记录 API 请求日志 | 22 | * 创建 ApiAccessLogFilter Bean,记录 API 请求日志 |
| 40 | */ | 23 | */ |
| @@ -42,8 +25,8 @@ public class YudaoApiLogAutoConfiguration implements WebMvcConfigurer { | @@ -42,8 +25,8 @@ public class YudaoApiLogAutoConfiguration implements WebMvcConfigurer { | ||
| 42 | @ConditionalOnProperty(prefix = "yudao.access-log", value = "enable", matchIfMissing = true) // 允许使用 yudao.access-log.enable=false 禁用访问日志 | 25 | @ConditionalOnProperty(prefix = "yudao.access-log", value = "enable", matchIfMissing = true) // 允许使用 yudao.access-log.enable=false 禁用访问日志 |
| 43 | public FilterRegistrationBean<ApiAccessLogFilter> apiAccessLogFilter(WebProperties webProperties, | 26 | public FilterRegistrationBean<ApiAccessLogFilter> apiAccessLogFilter(WebProperties webProperties, |
| 44 | @Value("${spring.application.name}") String applicationName, | 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 | return createFilterBean(filter, WebFilterOrderEnum.API_ACCESS_LOG_FILTER); | 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,7 +9,6 @@ import cn.hutool.core.util.BooleanUtil; | ||
| 9 | import cn.hutool.core.util.StrUtil; | 9 | import cn.hutool.core.util.StrUtil; |
| 10 | import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; | 10 | import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog; |
| 11 | import cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum; | 11 | import cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum; |
| 12 | -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; | ||
| 13 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; | 12 | import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; |
| 14 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 13 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 15 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | 14 | import cn.iocoder.yudao.framework.common.util.json.JsonUtils; |
| @@ -18,6 +17,7 @@ import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | @@ -18,6 +17,7 @@ import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | ||
| 18 | import cn.iocoder.yudao.framework.web.config.WebProperties; | 17 | import cn.iocoder.yudao.framework.web.config.WebProperties; |
| 19 | import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter; | 18 | import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter; |
| 20 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 19 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; |
| 20 | +import cn.iocoder.yudao.module.infra.api.logger.ApiAccessLogApi; | ||
| 21 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; | 21 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; |
| 22 | import com.fasterxml.jackson.databind.JsonNode; | 22 | import com.fasterxml.jackson.databind.JsonNode; |
| 23 | import io.swagger.v3.oas.annotations.Operation; | 23 | import io.swagger.v3.oas.annotations.Operation; |
| @@ -36,7 +36,7 @@ import java.time.temporal.ChronoUnit; | @@ -36,7 +36,7 @@ import java.time.temporal.ChronoUnit; | ||
| 36 | import java.util.Iterator; | 36 | import java.util.Iterator; |
| 37 | import java.util.Map; | 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 | import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString; | 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,12 +53,12 @@ public class ApiAccessLogFilter extends ApiRequestFilter { | ||
| 53 | 53 | ||
| 54 | private final String applicationName; | 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 | super(webProperties); | 59 | super(webProperties); |
| 60 | this.applicationName = applicationName; | 60 | this.applicationName = applicationName; |
| 61 | - this.apiAccessLogFrameworkService = apiAccessLogFrameworkService; | 61 | + this.apiAccessLogApi = apiAccessLogApi; |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | @Override | 64 | @Override |
| @@ -91,7 +91,7 @@ public class ApiAccessLogFilter extends ApiRequestFilter { | @@ -91,7 +91,7 @@ public class ApiAccessLogFilter extends ApiRequestFilter { | ||
| 91 | if (!enable) { | 91 | if (!enable) { |
| 92 | return; | 92 | return; |
| 93 | } | 93 | } |
| 94 | - apiAccessLogFrameworkService.createApiAccessLog(accessLog); | 94 | + apiAccessLogApi.createApiAccessLogAsync(accessLog); |
| 95 | } catch (Throwable th) { | 95 | } catch (Throwable th) { |
| 96 | log.error("[createApiAccessLog][url({}) log({}) 发生异常]", request.getRequestURI(), toJsonString(accessLog), th); | 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 | package cn.iocoder.yudao.framework.web.config; | 1 | package cn.iocoder.yudao.framework.web.config; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; | ||
| 4 | import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; | 3 | import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum; |
| 5 | import cn.iocoder.yudao.framework.web.core.filter.CacheRequestBodyFilter; | 4 | import cn.iocoder.yudao.framework.web.core.filter.CacheRequestBodyFilter; |
| 6 | import cn.iocoder.yudao.framework.web.core.filter.DemoFilter; | 5 | import cn.iocoder.yudao.framework.web.core.filter.DemoFilter; |
| 7 | import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; | 6 | import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler; |
| 8 | import cn.iocoder.yudao.framework.web.core.handler.GlobalResponseBodyHandler; | 7 | import cn.iocoder.yudao.framework.web.core.handler.GlobalResponseBodyHandler; |
| 9 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 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 | import org.springframework.beans.factory.annotation.Value; | 12 | import org.springframework.beans.factory.annotation.Value; |
| 11 | import org.springframework.boot.autoconfigure.AutoConfiguration; | 13 | import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 12 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | 14 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| @@ -25,9 +27,6 @@ import org.springframework.web.filter.CorsFilter; | @@ -25,9 +27,6 @@ import org.springframework.web.filter.CorsFilter; | ||
| 25 | import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; | 27 | import org.springframework.web.servlet.config.annotation.PathMatchConfigurer; |
| 26 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | 28 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; |
| 27 | 29 | ||
| 28 | -import jakarta.annotation.Resource; | ||
| 29 | -import jakarta.servlet.Filter; | ||
| 30 | - | ||
| 31 | @AutoConfiguration | 30 | @AutoConfiguration |
| 32 | @EnableConfigurationProperties(WebProperties.class) | 31 | @EnableConfigurationProperties(WebProperties.class) |
| 33 | public class YudaoWebAutoConfiguration implements WebMvcConfigurer { | 32 | public class YudaoWebAutoConfiguration implements WebMvcConfigurer { |
| @@ -59,8 +58,9 @@ public class YudaoWebAutoConfiguration implements WebMvcConfigurer { | @@ -59,8 +58,9 @@ public class YudaoWebAutoConfiguration implements WebMvcConfigurer { | ||
| 59 | } | 58 | } |
| 60 | 59 | ||
| 61 | @Bean | 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 | @Bean | 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,7 +5,6 @@ import cn.hutool.core.map.MapUtil; | ||
| 5 | import cn.hutool.core.util.ObjUtil; | 5 | import cn.hutool.core.util.ObjUtil; |
| 6 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| 7 | import cn.hutool.extra.servlet.JakartaServletUtil; | 7 | import cn.hutool.extra.servlet.JakartaServletUtil; |
| 8 | -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; | ||
| 9 | import cn.iocoder.yudao.framework.common.exception.ServiceException; | 8 | import cn.iocoder.yudao.framework.common.exception.ServiceException; |
| 10 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | 9 | import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; |
| 11 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 10 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| @@ -14,6 +13,7 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | @@ -14,6 +13,7 @@ import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 14 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; | 13 | import cn.iocoder.yudao.framework.common.util.monitor.TracerUtils; |
| 15 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | 14 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 16 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; | 15 | import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; |
| 16 | +import cn.iocoder.yudao.module.infra.api.logger.ApiErrorLogApi; | ||
| 17 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; | 17 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; |
| 18 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; | 18 | import com.fasterxml.jackson.databind.exc.InvalidFormatException; |
| 19 | import jakarta.servlet.http.HttpServletRequest; | 19 | import jakarta.servlet.http.HttpServletRequest; |
| @@ -40,12 +40,7 @@ import java.time.LocalDateTime; | @@ -40,12 +40,7 @@ import java.time.LocalDateTime; | ||
| 40 | import java.util.Map; | 40 | import java.util.Map; |
| 41 | import java.util.Set; | 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 | * 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号 | 46 | * 全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号 |
| @@ -65,7 +60,7 @@ public class GlobalExceptionHandler { | @@ -65,7 +60,7 @@ public class GlobalExceptionHandler { | ||
| 65 | @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") | 60 | @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") |
| 66 | private final String applicationName; | 61 | private final String applicationName; |
| 67 | 62 | ||
| 68 | - private final ApiErrorLogFrameworkService apiErrorLogFrameworkService; | 63 | + private final ApiErrorLogApi apiErrorLogApi; |
| 69 | 64 | ||
| 70 | /** | 65 | /** |
| 71 | * 处理所有异常,主要是提供给 Filter 使用 | 66 | * 处理所有异常,主要是提供给 Filter 使用 |
| @@ -288,7 +283,7 @@ public class GlobalExceptionHandler { | @@ -288,7 +283,7 @@ public class GlobalExceptionHandler { | ||
| 288 | // 初始化 errorLog | 283 | // 初始化 errorLog |
| 289 | buildExceptionLog(errorLog, req, e); | 284 | buildExceptionLog(errorLog, req, e); |
| 290 | // 执行插入 errorLog | 285 | // 执行插入 errorLog |
| 291 | - apiErrorLogFrameworkService.createApiErrorLog(errorLog); | 286 | + apiErrorLogApi.createApiErrorLogAsync(errorLog); |
| 292 | } catch (Throwable th) { | 287 | } catch (Throwable th) { |
| 293 | log.error("[createExceptionLog][url({}) log({}) 发生异常]", req.getRequestURI(), JsonUtils.toJsonString(errorLog), th); | 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,6 +3,7 @@ package cn.iocoder.yudao.module.infra.api.logger; | ||
| 3 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; | 3 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; |
| 4 | 4 | ||
| 5 | import jakarta.validation.Valid; | 5 | import jakarta.validation.Valid; |
| 6 | +import org.springframework.scheduling.annotation.Async; | ||
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| 8 | * API 访问日志的 API 接口 | 9 | * API 访问日志的 API 接口 |
| @@ -18,4 +19,14 @@ public interface ApiAccessLogApi { | @@ -18,4 +19,14 @@ public interface ApiAccessLogApi { | ||
| 18 | */ | 19 | */ |
| 19 | void createApiAccessLog(@Valid ApiAccessLogCreateReqDTO createDTO); | 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,6 +3,7 @@ package cn.iocoder.yudao.module.infra.api.logger; | ||
| 3 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; | 3 | import cn.iocoder.yudao.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; |
| 4 | 4 | ||
| 5 | import jakarta.validation.Valid; | 5 | import jakarta.validation.Valid; |
| 6 | +import org.springframework.scheduling.annotation.Async; | ||
| 6 | 7 | ||
| 7 | /** | 8 | /** |
| 8 | * API 错误日志的 API 接口 | 9 | * API 错误日志的 API 接口 |
| @@ -18,4 +19,14 @@ public interface ApiErrorLogApi { | @@ -18,4 +19,14 @@ public interface ApiErrorLogApi { | ||
| 18 | */ | 19 | */ |
| 19 | void createApiErrorLog(@Valid ApiErrorLogCreateReqDTO createDTO); | 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 | } |