diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogAutoConfiguration.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogAutoConfiguration.java index 3daeb38b7..df5498d15 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogAutoConfiguration.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogAutoConfiguration.java @@ -1,15 +1,10 @@ package com.cf.imes.framework.apilog.config; import com.cf.imes.framework.apilog.core.filter.ApiAccessLogFilter; -import com.cf.imes.framework.apilog.core.service.ApiAccessLogFrameworkService; -import com.cf.imes.framework.apilog.core.service.ApiAccessLogFrameworkServiceImpl; -import com.cf.imes.framework.apilog.core.service.ApiErrorLogFrameworkService; -import com.cf.imes.framework.apilog.core.service.ApiErrorLogFrameworkServiceImpl; import com.cf.imes.framework.common.enums.WebFilterOrderEnum; import com.cf.imes.framework.web.config.WebProperties; import com.cf.imes.framework.web.config.ChenfengWebAutoConfiguration; import com.cf.imes.module.infra.api.logger.ApiAccessLogApi; -import com.cf.imes.module.infra.api.logger.ApiErrorLogApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -21,16 +16,6 @@ import javax.servlet.Filter; @AutoConfiguration(after = ChenfengWebAutoConfiguration.class) public class ChenfengApiLogAutoConfiguration { - @Bean - public ApiAccessLogFrameworkService apiAccessLogFrameworkService(ApiAccessLogApi apiAccessLogApi) { - return new ApiAccessLogFrameworkServiceImpl(apiAccessLogApi); - } - - @Bean - public ApiErrorLogFrameworkService apiErrorLogFrameworkService(ApiErrorLogApi apiErrorLogApi) { - return new ApiErrorLogFrameworkServiceImpl(apiErrorLogApi); - } - /** * 创建 ApiAccessLogFilter Bean,记录 API 请求日志 */ @@ -38,8 +23,8 @@ public class ChenfengApiLogAutoConfiguration { @ConditionalOnProperty(prefix = "chenfeng.access-log", value = "enable", matchIfMissing = true) // 允许使用 chenfeng.access-log.enable=false 禁用访问日志 public FilterRegistrationBean apiAccessLogFilter(WebProperties webProperties, @Value("${spring.application.name}") String applicationName, - ApiAccessLogFrameworkService apiAccessLogFrameworkService) { - ApiAccessLogFilter filter = new ApiAccessLogFilter(webProperties, applicationName, apiAccessLogFrameworkService); + ApiAccessLogApi apiAccessLogApi) { + ApiAccessLogFilter filter = new ApiAccessLogFilter(webProperties, applicationName, apiAccessLogApi); return createFilterBean(filter, WebFilterOrderEnum.API_ACCESS_LOG_FILTER); } diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogRpcAutoConfiguration.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogRpcAutoConfiguration.java index 743777b1d..1cc5660cc 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogRpcAutoConfiguration.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/config/ChenfengApiLogRpcAutoConfiguration.java @@ -11,7 +11,6 @@ import org.springframework.cloud.openfeign.EnableFeignClients; * @author 晨丰科技 */ @AutoConfiguration -@EnableFeignClients(clients = {ApiAccessLogApi.class, // 主要是引入相关的 API 服务 - ApiErrorLogApi.class}) +@EnableFeignClients(clients = {ApiAccessLogApi.class, ApiErrorLogApi.class}) public class ChenfengApiLogRpcAutoConfiguration { } diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/filter/ApiAccessLogFilter.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/filter/ApiAccessLogFilter.java index cf53089cf..fc2a9f5d2 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/filter/ApiAccessLogFilter.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/filter/ApiAccessLogFilter.java @@ -3,8 +3,6 @@ package com.cf.imes.framework.apilog.core.filter; import cn.hutool.core.date.LocalDateTimeUtil; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.map.MapUtil; -import com.cf.imes.framework.apilog.core.service.ApiAccessLog; -import com.cf.imes.framework.apilog.core.service.ApiAccessLogFrameworkService; import com.cf.imes.framework.common.exception.enums.GlobalErrorCodeConstants; import com.cf.imes.framework.common.pojo.CommonResult; import com.cf.imes.framework.common.util.monitor.TracerUtils; @@ -12,6 +10,8 @@ import com.cf.imes.framework.common.util.servlet.ServletUtils; import com.cf.imes.framework.web.config.WebProperties; import com.cf.imes.framework.web.core.filter.ApiRequestFilter; import com.cf.imes.framework.web.core.util.WebFrameworkUtils; +import com.cf.imes.module.infra.api.logger.ApiAccessLogApi; +import com.cf.imes.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; import lombok.extern.slf4j.Slf4j; import javax.servlet.FilterChain; @@ -35,12 +35,12 @@ public class ApiAccessLogFilter extends ApiRequestFilter { private final String applicationName; - private final ApiAccessLogFrameworkService apiAccessLogFrameworkService; + private final ApiAccessLogApi apiAccessLogApi; - public ApiAccessLogFilter(WebProperties webProperties, String applicationName, ApiAccessLogFrameworkService apiAccessLogFrameworkService) { + public ApiAccessLogFilter(WebProperties webProperties, String applicationName, ApiAccessLogApi apiAccessLogApi) { super(webProperties); this.applicationName = applicationName; - this.apiAccessLogFrameworkService = apiAccessLogFrameworkService; + this.apiAccessLogApi = apiAccessLogApi; } @Override @@ -66,16 +66,16 @@ public class ApiAccessLogFilter extends ApiRequestFilter { private void createApiAccessLog(HttpServletRequest request, LocalDateTime beginTime, Map queryString, String requestBody, Exception ex) { - ApiAccessLog accessLog = new ApiAccessLog(); + ApiAccessLogCreateReqDTO accessLog = new ApiAccessLogCreateReqDTO(); try { this.buildApiAccessLogDTO(accessLog, request, beginTime, queryString, requestBody, ex); - apiAccessLogFrameworkService.createApiAccessLog(accessLog); + apiAccessLogApi.createApiAccessLogAsync(accessLog); } catch (Throwable th) { log.error("[createApiAccessLog][url({}) log({}) 发生异常]", request.getRequestURI(), toJsonString(accessLog), th); } } - private void buildApiAccessLogDTO(ApiAccessLog accessLog, HttpServletRequest request, LocalDateTime beginTime, + private void buildApiAccessLogDTO(ApiAccessLogCreateReqDTO accessLog, HttpServletRequest request, LocalDateTime beginTime, Map queryString, String requestBody, Exception ex) { // 处理用户信息 accessLog.setUserId(WebFrameworkUtils.getLoginUserId(request)); diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLog.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLog.java deleted file mode 100644 index 326ddc98d..000000000 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLog.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.cf.imes.framework.apilog.core.service; - -import lombok.Data; - -import javax.validation.constraints.NotNull; -import java.time.LocalDateTime; - -/** - * API 访问日志 - * - * @author 晨丰科技 - */ -@Data -public class ApiAccessLog { - - /** - * 链路追踪编号 - */ - private String traceId; - /** - * 用户编号 - */ - private Long userId; - /** - * 用户类型 - */ - private Integer userType; - /** - * 应用名 - */ - @NotNull(message = "应用名不能为空") - private String applicationName; - - /** - * 请求方法名 - */ - @NotNull(message = "http 请求方法不能为空") - private String requestMethod; - /** - * 访问地址 - */ - @NotNull(message = "访问地址不能为空") - private String requestUrl; - /** - * 请求参数 - */ - @NotNull(message = "请求参数不能为空") - private String requestParams; - /** - * 用户 IP - */ - @NotNull(message = "ip 不能为空") - private String userIp; - /** - * 浏览器 UA - */ - @NotNull(message = "User-Agent 不能为空") - private String userAgent; - - /** - * 开始请求时间 - */ - @NotNull(message = "开始请求时间不能为空") - private LocalDateTime beginTime; - /** - * 结束请求时间 - */ - @NotNull(message = "结束请求时间不能为空") - private LocalDateTime endTime; - /** - * 执行时长,单位:毫秒 - */ - @NotNull(message = "执行时长不能为空") - private Integer duration; - /** - * 结果码 - */ - @NotNull(message = "错误码不能为空") - private Integer resultCode; - /** - * 结果提示 - */ - private String resultMsg; - -} diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLogFrameworkService.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLogFrameworkService.java deleted file mode 100644 index 471fe27ea..000000000 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLogFrameworkService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.cf.imes.framework.apilog.core.service; - -/** - * API 访问日志 Framework Service 接口 - * - * @author 晨丰科技 - */ -public interface ApiAccessLogFrameworkService { - - /** - * 创建 API 访问日志 - * - * @param apiAccessLog API 访问日志 - */ - void createApiAccessLog(ApiAccessLog apiAccessLog); - -} diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java deleted file mode 100644 index b3194163d..000000000 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiAccessLogFrameworkServiceImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.cf.imes.framework.apilog.core.service; - -import cn.hutool.core.bean.BeanUtil; -import com.cf.imes.module.infra.api.logger.ApiAccessLogApi; -import com.cf.imes.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; -import lombok.RequiredArgsConstructor; -import org.springframework.scheduling.annotation.Async; - -/** - * API 访问日志 Framework Service 实现类 - * - * 基于 {@link ApiAccessLogApi} 远程服务,记录访问日志 - * - * @author 晨丰科技 - */ -@RequiredArgsConstructor -public class ApiAccessLogFrameworkServiceImpl implements ApiAccessLogFrameworkService { - - private final ApiAccessLogApi apiAccessLogApi; - - @Override - @Async - public void createApiAccessLog(ApiAccessLog apiAccessLog) { - ApiAccessLogCreateReqDTO reqDTO = BeanUtil.copyProperties(apiAccessLog, ApiAccessLogCreateReqDTO.class); - apiAccessLogApi.createApiAccessLog(reqDTO).checkError(); - } - -} diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLog.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLog.java deleted file mode 100644 index 7475b8045..000000000 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLog.java +++ /dev/null @@ -1,107 +0,0 @@ -package com.cf.imes.framework.apilog.core.service; - -import lombok.Data; - -import javax.validation.constraints.NotNull; -import java.time.LocalDateTime; - -/** - * API 错误日志 - * - * @author 晨丰科技 - */ -@Data -public class ApiErrorLog { - - /** - * 链路编号 - */ - private String traceId; - /** - * 账号编号 - */ - private Long userId; - /** - * 用户类型 - */ - private Integer userType; - /** - * 应用名 - */ - @NotNull(message = "应用名不能为空") - private String applicationName; - - /** - * 请求方法名 - */ - @NotNull(message = "http 请求方法不能为空") - private String requestMethod; - /** - * 访问地址 - */ - @NotNull(message = "访问地址不能为空") - private String requestUrl; - /** - * 请求参数 - */ - @NotNull(message = "请求参数不能为空") - private String requestParams; - /** - * 用户 IP - */ - @NotNull(message = "ip 不能为空") - private String userIp; - /** - * 浏览器 UA - */ - @NotNull(message = "User-Agent 不能为空") - private String userAgent; - - /** - * 异常时间 - */ - @NotNull(message = "异常时间不能为空") - private LocalDateTime exceptionTime; - /** - * 异常名 - */ - @NotNull(message = "异常名不能为空") - private String exceptionName; - /** - * 异常发生的类全名 - */ - @NotNull(message = "异常发生的类全名不能为空") - private String exceptionClassName; - /** - * 异常发生的类文件 - */ - @NotNull(message = "异常发生的类文件不能为空") - private String exceptionFileName; - /** - * 异常发生的方法名 - */ - @NotNull(message = "异常发生的方法名不能为空") - private String exceptionMethodName; - /** - * 异常发生的方法所在行 - */ - @NotNull(message = "异常发生的方法所在行不能为空") - private Integer exceptionLineNumber; - /** - * 异常的栈轨迹异常的栈轨迹 - */ - @NotNull(message = "异常的栈轨迹不能为空") - private String exceptionStackTrace; - /** - * 异常导致的根消息 - */ - @NotNull(message = "异常导致的根消息不能为空") - private String exceptionRootCauseMessage; - /** - * 异常导致的消息 - */ - @NotNull(message = "异常导致的消息不能为空") - private String exceptionMessage; - - -} diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLogFrameworkService.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLogFrameworkService.java deleted file mode 100644 index 2d5c1c780..000000000 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLogFrameworkService.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.cf.imes.framework.apilog.core.service; - -/** - * API 错误日志 Framework Service 接口 - * - * @author 晨丰科技 - */ -public interface ApiErrorLogFrameworkService { - - /** - * 创建 API 错误日志 - * - * @param apiErrorLog API 错误日志 - */ - void createApiErrorLog(ApiErrorLog apiErrorLog); - -} diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java deleted file mode 100644 index 7eba3625c..000000000 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/apilog/core/service/ApiErrorLogFrameworkServiceImpl.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.cf.imes.framework.apilog.core.service; - -import cn.hutool.core.bean.BeanUtil; -import com.cf.imes.module.infra.api.logger.ApiErrorLogApi; -import com.cf.imes.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; -import lombok.RequiredArgsConstructor; -import org.springframework.scheduling.annotation.Async; - -/** - * API 错误日志 Framework Service 实现类 - * - * 基于 {@link ApiErrorLogApi} 远程服务,记录错误日志 - * - * @author 晨丰科技 - */ -@RequiredArgsConstructor -public class ApiErrorLogFrameworkServiceImpl implements ApiErrorLogFrameworkService { - - private final ApiErrorLogApi apiErrorLogApi; - - @Override - @Async - public void createApiErrorLog(ApiErrorLog apiErrorLog) { - ApiErrorLogCreateReqDTO reqDTO = BeanUtil.copyProperties(apiErrorLog, ApiErrorLogCreateReqDTO.class); - apiErrorLogApi.createApiErrorLog(reqDTO).checkError(); - } - -} diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/config/ChenfengWebAutoConfiguration.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/config/ChenfengWebAutoConfiguration.java index e34d08f56..bc0df4635 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/config/ChenfengWebAutoConfiguration.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/config/ChenfengWebAutoConfiguration.java @@ -1,13 +1,13 @@ package com.cf.imes.framework.web.config; import com.cf.imes.framework.aop.ControllerResolver; -import com.cf.imes.framework.apilog.core.service.ApiErrorLogFrameworkService; import com.cf.imes.framework.common.enums.WebFilterOrderEnum; import com.cf.imes.framework.web.core.filter.CacheRequestBodyFilter; import com.cf.imes.framework.web.core.filter.DataSourceFilter; import com.cf.imes.framework.web.core.handler.GlobalExceptionHandler; import com.cf.imes.framework.web.core.handler.GlobalResponseBodyHandler; import com.cf.imes.framework.web.core.util.WebFrameworkUtils; +import com.cf.imes.module.infra.api.logger.ApiErrorLogApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -59,8 +59,8 @@ public class ChenfengWebAutoConfiguration implements WebMvcConfigurer { } @Bean - public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogFrameworkService ApiErrorLogFrameworkService) { - return new GlobalExceptionHandler(applicationName, ApiErrorLogFrameworkService); + public GlobalExceptionHandler globalExceptionHandler(ApiErrorLogApi apiErrorLogApi) { + return new GlobalExceptionHandler(applicationName, apiErrorLogApi); } @Bean diff --git a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java index a3782a321..0ae4cfe59 100644 --- a/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java +++ b/cf-framework/cf-spring-boot-starter-web/src/main/java/com/cf/imes/framework/web/core/handler/GlobalExceptionHandler.java @@ -3,15 +3,14 @@ package com.cf.imes.framework.web.core.handler; import cn.hutool.core.exceptions.ExceptionUtil; import cn.hutool.core.map.MapUtil; import cn.hutool.core.text.CharSequenceUtil; -import cn.hutool.extra.servlet.ServletUtil; -import com.cf.imes.framework.apilog.core.service.ApiErrorLog; -import com.cf.imes.framework.apilog.core.service.ApiErrorLogFrameworkService; import com.cf.imes.framework.common.exception.ServiceException; import com.cf.imes.framework.common.pojo.CommonResult; import com.cf.imes.framework.common.util.monitor.TracerUtils; import com.cf.imes.framework.web.core.util.WebFrameworkUtils; import com.cf.imes.framework.common.util.json.JsonUtils; import com.cf.imes.framework.common.util.servlet.ServletUtils; +import com.cf.imes.module.infra.api.logger.ApiErrorLogApi; +import com.cf.imes.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.dao.DuplicateKeyException; @@ -50,7 +49,7 @@ public class GlobalExceptionHandler { private final String applicationName; - private final ApiErrorLogFrameworkService apiErrorLogFrameworkService; + private final ApiErrorLogApi apiErrorLogApi; /** * 处理所有异常,主要是提供给 Filter 使用 @@ -251,14 +250,6 @@ public class GlobalExceptionHandler { return tableNotExistsResult; } - if(ex instanceof NullPointerException){ - // 处理异常 - log.error("[defaultExceptionHandler]", ex); - // 插入异常日志 - this.createExceptionLog(req, ex); - return CommonResult.error(DATA_EXCEPTION_ERROR.getCode(), DATA_EXCEPTION_ERROR.getMsg()); - } - // 情况二:部分特殊的库的处理 if (Objects.equals("io.github.resilience4j.ratelimiter.RequestNotPermitted", ex.getClass().getName())) { return requestNotPermittedExceptionHandler(req, ex); @@ -274,18 +265,18 @@ public class GlobalExceptionHandler { private void createExceptionLog(HttpServletRequest req, Throwable e) { // 插入错误日志 - ApiErrorLog errorLog = new ApiErrorLog(); + ApiErrorLogCreateReqDTO errorLog = new ApiErrorLogCreateReqDTO(); try { // 初始化 errorLog - initExceptionLog(errorLog, req, e); + buildExceptionLog(errorLog, req, e); // 执行插入 errorLog - apiErrorLogFrameworkService.createApiErrorLog(errorLog); + apiErrorLogApi.createApiErrorLogAsync(errorLog); } catch (Throwable th) { log.error("[createExceptionLog][url({}) log({}) 发生异常]", req.getRequestURI(), JsonUtils.toJsonString(errorLog), th); } } - private void initExceptionLog(ApiErrorLog errorLog, HttpServletRequest request, Throwable e) { + private void buildExceptionLog(ApiErrorLogCreateReqDTO errorLog, HttpServletRequest request, Throwable e) { // 处理用户信息 errorLog.setUserId(WebFrameworkUtils.getLoginUserId(request)); errorLog.setUserType(WebFrameworkUtils.getLoginUserType(request)); @@ -306,12 +297,12 @@ public class GlobalExceptionHandler { errorLog.setApplicationName(applicationName); errorLog.setRequestUrl(request.getRequestURI()); Map requestParams = MapUtil.builder() - .put("query", ServletUtil.getParamMap(request)) - .put("body", ServletUtil.getBody(request)).build(); + .put("query", ServletUtils.getParamMap(request)) + .put("body", ServletUtils.getBody(request)).build(); errorLog.setRequestParams(JsonUtils.toJsonString(requestParams)); errorLog.setRequestMethod(request.getMethod()); errorLog.setUserAgent(ServletUtils.getUserAgent(request)); - errorLog.setUserIp(ServletUtil.getClientIP(request)); + errorLog.setUserIp(ServletUtils.getClientIP(request)); errorLog.setExceptionTime(LocalDateTime.now()); } diff --git a/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiAccessLogApi.java b/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiAccessLogApi.java index 984736286..678ea53d4 100644 --- a/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiAccessLogApi.java +++ b/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiAccessLogApi.java @@ -6,6 +6,7 @@ import com.cf.imes.module.infra.enums.ApiConstants; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.Operation; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -21,4 +22,13 @@ public interface ApiAccessLogApi { @Operation(summary = "创建 API 访问日志") CommonResult createApiAccessLog(@Valid @RequestBody ApiAccessLogCreateReqDTO createDTO); + /** + * 【异步】创建 API 访问日志 + * + * @param createDTO 访问日志 DTO + */ + @Async + default void createApiAccessLogAsync(ApiAccessLogCreateReqDTO createDTO) { + createApiAccessLog(createDTO).checkError(); + } } diff --git a/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiErrorLogApi.java b/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiErrorLogApi.java index c54586a4f..7568d25e0 100644 --- a/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiErrorLogApi.java +++ b/cf-module-infra/cf-module-infra-api/src/main/java/com/cf/imes/module/infra/api/logger/ApiErrorLogApi.java @@ -6,6 +6,7 @@ import com.cf.imes.module.infra.enums.ApiConstants; import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.Operation; import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.scheduling.annotation.Async; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -21,4 +22,8 @@ public interface ApiErrorLogApi { @Operation(summary = "创建 API 异常日志") CommonResult createApiErrorLog(@Valid @RequestBody ApiErrorLogCreateReqDTO createDTO); + @Async + default void createApiErrorLogAsync(ApiErrorLogCreateReqDTO createDTO) { + createApiErrorLog(createDTO).checkError(); + } } diff --git a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiAccessLogDO.java b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiAccessLogDO.java index 243fe084b..f4892aa73 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiAccessLogDO.java +++ b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiAccessLogDO.java @@ -25,6 +25,16 @@ import java.time.LocalDateTime; @AllArgsConstructor public class ApiAccessLogDO extends BaseDO { + /** + * {@link #requestParams} 的最大长度 + */ + public static final Integer REQUEST_PARAMS_MAX_LENGTH = 8000; + + /** + * {@link #resultMsg} 的最大长度 + */ + public static final Integer RESULT_MSG_MAX_LENGTH = 512; + /** * 编号 */ diff --git a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiErrorLogDO.java b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiErrorLogDO.java index ddaa6cc7d..e17434517 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiErrorLogDO.java +++ b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/dal/dataobject/logger/ApiErrorLogDO.java @@ -25,6 +25,11 @@ import java.time.LocalDateTime; @KeySequence(value = "infra_api_error_log_seq") public class ApiErrorLogDO extends BaseDO { + /** + * {@link #requestParams} 的最大长度 + */ + public static final Integer REQUEST_PARAMS_MAX_LENGTH = 8000; + /** * 编号 */ diff --git a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiAccessLogServiceImpl.java b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiAccessLogServiceImpl.java index ac886b21a..0066867ee 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiAccessLogServiceImpl.java +++ b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiAccessLogServiceImpl.java @@ -1,7 +1,10 @@ package com.cf.imes.module.infra.service.logger; +import cn.hutool.core.util.StrUtil; import com.cf.imes.framework.common.pojo.PageResult; import com.cf.imes.framework.common.util.object.BeanUtils; +import com.cf.imes.framework.organ.core.context.OrganContextHolder; +import com.cf.imes.framework.organ.core.util.OrganUtils; import com.cf.imes.module.infra.api.logger.dto.ApiAccessLogCreateReqDTO; import com.cf.imes.module.infra.controller.admin.logger.vo.apiaccesslog.ApiAccessLogPageReqVO; import com.cf.imes.module.infra.dal.dataobject.logger.ApiAccessLogDO; @@ -13,6 +16,9 @@ import org.springframework.validation.annotation.Validated; import javax.annotation.Resource; import java.time.LocalDateTime; +import static com.cf.imes.module.infra.dal.dataobject.logger.ApiAccessLogDO.REQUEST_PARAMS_MAX_LENGTH; +import static com.cf.imes.module.infra.dal.dataobject.logger.ApiAccessLogDO.RESULT_MSG_MAX_LENGTH; + /** * API 访问日志 Service 实现类 * @@ -29,10 +35,13 @@ public class ApiAccessLogServiceImpl implements ApiAccessLogService { @Override public void createApiAccessLog(ApiAccessLogCreateReqDTO createDTO) { ApiAccessLogDO apiAccessLog = BeanUtils.toBean(createDTO, ApiAccessLogDO.class); - try { + apiAccessLog.setRequestParams(StrUtil.maxLength(apiAccessLog.getRequestParams(), REQUEST_PARAMS_MAX_LENGTH)); + apiAccessLog.setResultMsg(StrUtil.maxLength(apiAccessLog.getResultMsg(), RESULT_MSG_MAX_LENGTH)); + if (OrganContextHolder.getOrganId() != null) { apiAccessLogMapper.insert(apiAccessLog); - }catch (Exception e) { - log.error("插入API 访问日志发生异常,异常信息{},日志内容{}", e.getMessage(), apiAccessLog); + } else { + // 极端情况下,上下文中没有租户时,此时忽略租户上下文,避免插入失败! + OrganUtils.executeIgnore(() -> apiAccessLogMapper.insert(apiAccessLog)); } } diff --git a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiErrorLogServiceImpl.java b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiErrorLogServiceImpl.java index faf39f684..67b13914a 100644 --- a/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiErrorLogServiceImpl.java +++ b/cf-module-infra/cf-module-infra-biz/src/main/java/com/cf/imes/module/infra/service/logger/ApiErrorLogServiceImpl.java @@ -1,7 +1,10 @@ package com.cf.imes.module.infra.service.logger; +import cn.hutool.core.util.StrUtil; import com.cf.imes.framework.common.pojo.PageResult; import com.cf.imes.framework.common.util.object.BeanUtils; +import com.cf.imes.framework.organ.core.context.OrganContextHolder; +import com.cf.imes.framework.organ.core.util.OrganUtils; import com.cf.imes.module.infra.api.logger.dto.ApiErrorLogCreateReqDTO; import com.cf.imes.module.infra.controller.admin.logger.vo.apierrorlog.ApiErrorLogPageReqVO; import com.cf.imes.module.infra.dal.dataobject.logger.ApiErrorLogDO; @@ -15,6 +18,7 @@ import javax.annotation.Resource; import java.time.LocalDateTime; import static com.cf.imes.framework.common.exception.util.ServiceExceptionUtil.exception; +import static com.cf.imes.module.infra.dal.dataobject.logger.ApiErrorLogDO.REQUEST_PARAMS_MAX_LENGTH; import static com.cf.imes.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_NOT_FOUND; import static com.cf.imes.module.infra.enums.ErrorCodeConstants.API_ERROR_LOG_PROCESSED; @@ -35,10 +39,12 @@ public class ApiErrorLogServiceImpl implements ApiErrorLogService { public void createApiErrorLog(ApiErrorLogCreateReqDTO createDTO) { ApiErrorLogDO apiErrorLog = BeanUtils.toBean(createDTO, ApiErrorLogDO.class) .setProcessStatus(ApiErrorLogProcessStatusEnum.INIT.getStatus()); - try { + apiErrorLog.setRequestParams(StrUtil.maxLength(apiErrorLog.getRequestParams(), REQUEST_PARAMS_MAX_LENGTH)); + if (OrganContextHolder.getOrganId() != null) { apiErrorLogMapper.insert(apiErrorLog); - }catch (Exception e) { - log.error("插入系统异常日志发生异常,异常信息{},日志内容{}",e.getMessage(), apiErrorLog); + } else { + // 极端情况下,上下文中没有租户时,此时忽略租户上下文,避免插入失败! + OrganUtils.executeIgnore(() -> apiErrorLogMapper.insert(apiErrorLog)); } }