diff --git a/src/main/java/com/cool/core/config/LogDiscardPolicy.java b/src/main/java/com/cool/core/config/LogDiscardPolicy.java new file mode 100644 index 0000000..a661aaa --- /dev/null +++ b/src/main/java/com/cool/core/config/LogDiscardPolicy.java @@ -0,0 +1,13 @@ +package com.cool.core.config; + +import java.util.concurrent.RejectedExecutionHandler; +import java.util.concurrent.ThreadPoolExecutor; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class LogDiscardPolicy implements RejectedExecutionHandler { + + public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { + log.warn("logTaskExecutor 当前已超过线程池最大队列容量,拒绝策略为丢弃该线程 {}", r.toString()); + } +} \ No newline at end of file diff --git a/src/main/java/com/cool/core/config/LogProperties.java b/src/main/java/com/cool/core/config/LogProperties.java new file mode 100644 index 0000000..0452ff8 --- /dev/null +++ b/src/main/java/com/cool/core/config/LogProperties.java @@ -0,0 +1,28 @@ +package com.cool.core.config; + +import lombok.Data; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +@Data +@Component +@ConfigurationProperties(prefix = "cool.log") +public class LogProperties { + + /** + * 请求参数最大字节,超过请求参数不记录 + */ + private int maxByteLength; + /** + * 核心线程数的倍数 + */ + private int corePoolSizeMultiplier; + /** + * 最大线程数的倍数 + */ + private int maxPoolSizeMultiplier; + /** + * 队列容量的倍数 + */ + private int queueCapacityMultiplier; +} diff --git a/src/main/java/com/cool/core/config/ThreadPoolConfig.java b/src/main/java/com/cool/core/config/ThreadPoolConfig.java new file mode 100644 index 0000000..b7bc67a --- /dev/null +++ b/src/main/java/com/cool/core/config/ThreadPoolConfig.java @@ -0,0 +1,33 @@ +package com.cool.core.config; + +import java.util.concurrent.Executor; +import lombok.RequiredArgsConstructor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +@RequiredArgsConstructor +public class ThreadPoolConfig { + + private final LogProperties logProperties; + + @Bean(name = "logTaskExecutor") + public Executor loggingTaskExecutor() { + ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); + + int corePoolSize = Runtime.getRuntime().availableProcessors() * logProperties.getCorePoolSizeMultiplier(); + int maxPoolSize = corePoolSize * logProperties.getMaxPoolSizeMultiplier(); + int queueCapacity = maxPoolSize * logProperties.getQueueCapacityMultiplier(); + + executor.setCorePoolSize(corePoolSize); + executor.setMaxPoolSize(maxPoolSize); + executor.setQueueCapacity(queueCapacity); + executor.setThreadNamePrefix("logTask-"); + + // 自定义拒绝策略 + executor.setRejectedExecutionHandler(new LogDiscardPolicy()); + executor.initialize(); + return executor; + } +} diff --git a/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java b/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java index decbc4a..877969d 100644 --- a/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java +++ b/src/main/java/com/cool/modules/base/service/sys/impl/BaseSysLogServiceImpl.java @@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.cool.core.base.BaseServiceImpl; +import com.cool.core.config.LogProperties; import com.cool.core.security.IgnoredUrlsProperties; import com.cool.core.util.CoolSecurityUtil; import com.cool.core.util.IPUtils; @@ -20,9 +21,8 @@ import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.query.QueryWrapper; import jakarta.servlet.http.HttpServletRequest; import java.util.Date; +import java.util.concurrent.Executor; import lombok.RequiredArgsConstructor; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; /** @@ -39,8 +39,10 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl maxJsonLength) { - // 超过指定 - newJSONObject.clear(); - } - recordAsync(requestURI, ipAddr, userId, newJSONObject); + // 异步记录日志 + recordAsync(ipAddr, requestURI, requestParams); } private boolean isIgnoreUrl(String requestURI) { @@ -99,16 +87,32 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl antPathMatcher.match(url, requestURI)); } + public void recordAsync(String ipAddr, String requestURI, JSONObject requestParams) { + logTaskExecutor.execute(() -> { + JSONObject userInfo = CoolSecurityUtil.getAdminUserInfo(requestParams); - @Async - public void recordAsync(String requestURI, String ip, Long userId, JSONObject params) { - BaseSysLogEntity logEntity = new BaseSysLogEntity(); - logEntity.setAction(requestURI); - logEntity.setIp(ip); - if (userId != null) { - logEntity.setUserId(userId); - } - logEntity.setParams(params); - save(logEntity); + Long userId = null; + if (userInfo != null) { + userId = userInfo.getLong("userId"); + } + + JSONObject newJSONObject = JSONUtil.parseObj(JSONUtil.toJsonStr(requestParams)); + newJSONObject.remove("tokenInfo"); + newJSONObject.remove("refreshToken"); + newJSONObject.remove("body"); + if (newJSONObject.toString().getBytes().length > logProperties.getMaxByteLength()) { + // 超过指定 + newJSONObject.clear(); + } + BaseSysLogEntity logEntity = new BaseSysLogEntity(); + logEntity.setAction(requestURI); + logEntity.setIp(ipAddr); + if (userId != null) { + logEntity.setUserId(userId); + } + logEntity.setParams(newJSONObject); + save(logEntity); + + }); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 1700b88..86f4429 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -2,6 +2,9 @@ server: port: 8001 servlet: context-path: / + compression: + enabled: true + mime-types: application/json,application/xml,text/html,text/plain spring: application: @@ -136,7 +139,14 @@ cool: base-url: http://127.0.0.1:${server.port}/upload # 系统日志请求参数超过1024字节 就不记录,避免日志过大 log: - maxJsonLength: 1024 + # 请求参数最大字节,超过请求参数不记录 + max-byte-length: 1024 + # 核心线程数的倍数 + core-pool-size-multiplier: 2 + # 最大线程数的倍数 + max-pool-size-multiplier: 3 + # 队列容量的倍数 + queue-capacity-multiplier: 3 # AutoTable配置,根据实体类自动生成表 auto-table: show-banner: false