From 7cf6dd9d0f937e82629f5f4f285bc2a060cf0cd6 Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Tue, 14 May 2024 23:44:11 +0800 Subject: [PATCH 01/10] docs: update README version to 1.6.3-SNAPSHOT --- README.md | 2 +- log-record-core/pom.xml | 2 +- log-record-springboot3-starter/pom.xml | 4 ++-- log-record-starter/pom.xml | 4 ++-- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 5c856fd..9e31f71 100644 --- a/README.md +++ b/README.md @@ -893,7 +893,7 @@ public void testBizIdWithSpEL(String bizId) { 应用之间通过关键操作的日志消息,互相通知。 -## 附录:Demo +## Demo 当你觉得用法不熟悉,可以查看单元测试用例,里面有最为详细且最全的使用示例。 diff --git a/log-record-core/pom.xml b/log-record-core/pom.xml index e06fff8..9aa1a66 100644 --- a/log-record-core/pom.xml +++ b/log-record-core/pom.xml @@ -10,7 +10,7 @@ cn.monitor4all log-record-core - 1.6.2 + 1.6.3-SNAPSHOT 8 diff --git a/log-record-springboot3-starter/pom.xml b/log-record-springboot3-starter/pom.xml index 1fba930..876abfc 100644 --- a/log-record-springboot3-starter/pom.xml +++ b/log-record-springboot3-starter/pom.xml @@ -10,7 +10,7 @@ cn.monitor4all log-record-springboot3-starter - 1.6.2 + 1.6.3-SNAPSHOT 17 @@ -35,7 +35,7 @@ cn.monitor4all log-record-core - 1.6.2 + 1.6.3-SNAPSHOT diff --git a/log-record-starter/pom.xml b/log-record-starter/pom.xml index dd13e02..cb904c9 100644 --- a/log-record-starter/pom.xml +++ b/log-record-starter/pom.xml @@ -10,7 +10,7 @@ cn.monitor4all log-record-starter - 1.6.2 + 1.6.3-SNAPSHOT 8 @@ -35,7 +35,7 @@ cn.monitor4all log-record-core - 1.6.2 + 1.6.3-SNAPSHOT From 1f66a679581b5e66efd76823ec8b5ed14d8abc37 Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Wed, 15 May 2024 23:37:53 +0800 Subject: [PATCH 02/10] fix: ci.yml update Install current version to local repository --- .github/workflows/ci.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7129abe..688959f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -23,6 +23,9 @@ jobs: path: ~/.m2 key: ${{ env.cache-name }}-${{ hashFiles('./log-record-starter/pom.xml') }} restore-keys: ${{ env.cache-name }}- + - name: Install current version to local repository + run: mvn install -DskipTests + working-directory: ./log-record-core - name: Test with Maven working-directory: ./log-record-starter run: mvn -V --no-transfer-progress test From abcef6342d03a3dc40c8e5089f672c282466ee77 Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Wed, 15 May 2024 23:45:28 +0800 Subject: [PATCH 03/10] fix: ci.yml update Install current version to local repository --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 688959f..1e908a4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,7 +24,7 @@ jobs: key: ${{ env.cache-name }}-${{ hashFiles('./log-record-starter/pom.xml') }} restore-keys: ${{ env.cache-name }}- - name: Install current version to local repository - run: mvn install -DskipTests + run: mvn install -DskipTests -Dgpg.skip working-directory: ./log-record-core - name: Test with Maven working-directory: ./log-record-starter From 18244a2a2ea49eba32bd101d2d53d6d5ec5d7ce4 Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Wed, 15 May 2024 23:51:08 +0800 Subject: [PATCH 04/10] =?UTF-8?q?fix:=20=E6=8E=92=E9=99=A4JacocoData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log-record-core/pom.xml | 5 +++++ log-record-springboot3-starter/pom.xml | 5 +++++ log-record-starter/pom.xml | 5 +++++ 3 files changed, 15 insertions(+) diff --git a/log-record-core/pom.xml b/log-record-core/pom.xml index 9aa1a66..e8ef367 100644 --- a/log-record-core/pom.xml +++ b/log-record-core/pom.xml @@ -210,6 +210,11 @@ report + + + **/*$jacocoData + + diff --git a/log-record-springboot3-starter/pom.xml b/log-record-springboot3-starter/pom.xml index 876abfc..f7fc581 100644 --- a/log-record-springboot3-starter/pom.xml +++ b/log-record-springboot3-starter/pom.xml @@ -154,6 +154,11 @@ report + + + **/*$jacocoData + + diff --git a/log-record-starter/pom.xml b/log-record-starter/pom.xml index cb904c9..ccbe1a6 100644 --- a/log-record-starter/pom.xml +++ b/log-record-starter/pom.xml @@ -153,6 +153,11 @@ report + + + **/*$jacocoData + + From db0d3782d43099170f3308f41341a50ca1aab07a Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Thu, 16 May 2024 22:30:10 +0800 Subject: [PATCH 05/10] =?UTF-8?q?fix:=20DIFF=E8=BF=87=E6=BB=A4=E5=90=88?= =?UTF-8?q?=E6=88=90=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- log-record-core/pom.xml | 5 ----- .../logRecord/function/CustomFunctionObjectDiff.java | 6 +++++- log-record-springboot3-starter/pom.xml | 5 ----- log-record-starter/pom.xml | 5 ----- 4 files changed, 5 insertions(+), 16 deletions(-) diff --git a/log-record-core/pom.xml b/log-record-core/pom.xml index e8ef367..9aa1a66 100644 --- a/log-record-core/pom.xml +++ b/log-record-core/pom.xml @@ -210,11 +210,6 @@ report - - - **/*$jacocoData - - diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/function/CustomFunctionObjectDiff.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/function/CustomFunctionObjectDiff.java index 5f88eab..5f79b10 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/function/CustomFunctionObjectDiff.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/function/CustomFunctionObjectDiff.java @@ -260,7 +260,11 @@ private static boolean isJsonArray(Object obj) { private static Field[] getAllFields(Class type) { List fields = new ArrayList<>(); for (Class c = type; c != null && !c.isSynthetic(); c = c.getSuperclass()) { - Collections.addAll(fields, c.getDeclaredFields()); + for (Field field : c.getDeclaredFields()) { + if (!field.isSynthetic()) { + fields.add(field); + } + } } return fields.toArray(new Field[0]); } diff --git a/log-record-springboot3-starter/pom.xml b/log-record-springboot3-starter/pom.xml index f7fc581..876abfc 100644 --- a/log-record-springboot3-starter/pom.xml +++ b/log-record-springboot3-starter/pom.xml @@ -154,11 +154,6 @@ report - - - **/*$jacocoData - - diff --git a/log-record-starter/pom.xml b/log-record-starter/pom.xml index ccbe1a6..cb904c9 100644 --- a/log-record-starter/pom.xml +++ b/log-record-starter/pom.xml @@ -153,11 +153,6 @@ report - - - **/*$jacocoData - - From 125dd01916664965b7837f8aadc52a52b986c126 Mon Sep 17 00:00:00 2001 From: zhendongyang Date: Sat, 25 May 2024 18:51:04 +0800 Subject: [PATCH 06/10] fix: safeToJson, use AbortPolicy for default thread pool --- .../logRecord/aop/SystemLogAspect.java | 7 +++---- .../impl/RabbitMqDataPipelineServiceImpl.java | 4 ++-- .../impl/RocketMqDataPipelineServiceImpl.java | 4 ++-- .../logRecord/thread/LogRecordThreadPool.java | 2 +- .../monitor4all/logRecord/util/JsonUtil.java | 18 ++++++++++++++++++ 5 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 log-record-core/src/main/java/cn/monitor4all/logRecord/util/JsonUtil.java diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/aop/SystemLogAspect.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/aop/SystemLogAspect.java index e2c4120..08cdc4c 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/aop/SystemLogAspect.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/aop/SystemLogAspect.java @@ -10,8 +10,7 @@ import cn.monitor4all.logRecord.service.IOperatorIdGetService; import cn.monitor4all.logRecord.service.LogRecordErrorHandlerService; import cn.monitor4all.logRecord.thread.LogRecordThreadPool; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.serializer.SerializerFeature; +import cn.monitor4all.logRecord.util.JsonUtil; import com.alibaba.ttl.TtlRunnable; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; @@ -128,7 +127,7 @@ public Object doAround(ProceedingJoinPoint pjp) throws Throwable { logDTO.setSuccess(true); } if (annotation.recordReturnValue() && result != null) { - logDTO.setReturnStr(JSON.toJSONString(result)); + logDTO.setReturnStr(JsonUtil.safeToJsonString(result)); } }); } catch (Throwable throwableAfterFuncSuccess) { @@ -305,7 +304,7 @@ private String parseParamToStringOrJson(String spel, StandardEvaluationContext c Expression msgExpression = parser.parseExpression(spel); Object obj = msgExpression.getValue(context, Object.class); if (obj != null) { - return obj instanceof String ? (String) obj : JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue); + return obj instanceof String ? (String) obj : JsonUtil.safeToJsonString(obj); } return null; } diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RabbitMqDataPipelineServiceImpl.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RabbitMqDataPipelineServiceImpl.java index a47c2cb..2054564 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RabbitMqDataPipelineServiceImpl.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RabbitMqDataPipelineServiceImpl.java @@ -4,7 +4,7 @@ import cn.monitor4all.logRecord.configuration.LogRecordProperties; import cn.monitor4all.logRecord.constants.LogConstants; import cn.monitor4all.logRecord.service.DataPipelineService; -import com.alibaba.fastjson.JSON; +import cn.monitor4all.logRecord.util.JsonUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -27,7 +27,7 @@ public class RabbitMqDataPipelineServiceImpl implements DataPipelineService { @Override public boolean createLog(LogDTO logDTO) { log.info("LogRecord RabbitMq ready to send routingKey [{}] LogDTO [{}]", properties.getRabbitMqProperties().getRoutingKey(), logDTO); - rubeExchangeTemplate.convertAndSend(properties.getRabbitMqProperties().getRoutingKey(), JSON.toJSONString(logDTO)); + rubeExchangeTemplate.convertAndSend(properties.getRabbitMqProperties().getRoutingKey(), JsonUtil.safeToJsonString(logDTO)); return true; } } diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RocketMqDataPipelineServiceImpl.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RocketMqDataPipelineServiceImpl.java index 3eb2b33..ad23c97 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RocketMqDataPipelineServiceImpl.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/service/impl/RocketMqDataPipelineServiceImpl.java @@ -4,7 +4,7 @@ import cn.monitor4all.logRecord.configuration.LogRecordProperties; import cn.monitor4all.logRecord.constants.LogConstants; import cn.monitor4all.logRecord.service.DataPipelineService; -import com.alibaba.fastjson.JSON; +import cn.monitor4all.logRecord.util.JsonUtil; import lombok.extern.slf4j.Slf4j; import org.apache.rocketmq.client.producer.DefaultMQProducer; import org.apache.rocketmq.client.producer.SendResult; @@ -30,7 +30,7 @@ public class RocketMqDataPipelineServiceImpl implements DataPipelineService { @Override public boolean createLog(LogDTO logDTO) { try { - Message msg = new Message(properties.getRocketMqProperties().getTopic(), properties.getRocketMqProperties().getTag(), (JSON.toJSONString(logDTO)).getBytes(RemotingHelper.DEFAULT_CHARSET)); + Message msg = new Message(properties.getRocketMqProperties().getTopic(), properties.getRocketMqProperties().getTag(), (JsonUtil.safeToJsonString(logDTO)).getBytes(RemotingHelper.DEFAULT_CHARSET)); SendResult sendResult = defaultMqProducer.send(msg); log.info("LogRecord RocketMq send LogDTO [{}] sendResult: [{}]", logDTO, sendResult); return true; diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java index b0a402a..22d8d11 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java @@ -22,7 +22,7 @@ public class LogRecordThreadPool { public LogRecordThreadPool(LogRecordProperties logRecordProperties) { log.info("LogRecordThreadPool init poolSize [{}]", logRecordProperties.getThreadPool().getPoolSize()); int poolSize = logRecordProperties.getThreadPool().getPoolSize(); - this.LOG_RECORD_POOL_EXECUTOR = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.CallerRunsPolicy()); + this.LOG_RECORD_POOL_EXECUTOR = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); } public ExecutorService getLogRecordPoolExecutor() { diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/util/JsonUtil.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/util/JsonUtil.java new file mode 100644 index 0000000..56a97a9 --- /dev/null +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/util/JsonUtil.java @@ -0,0 +1,18 @@ +package cn.monitor4all.logRecord.util; + + +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class JsonUtil { + + public static String safeToJsonString(Object object) { + try { + return JSON.toJSONString(object); + } catch (Exception e) { + log.error("safeToJsonString error, object {}", object, e); + return object.toString(); + } + } +} From 48645c1332ab062559b29829ad3c42e10b529ace Mon Sep 17 00:00:00 2001 From: zhendongyang Date: Sat, 25 May 2024 19:44:50 +0800 Subject: [PATCH 07/10] fix: user defined thread pool support --- .../thread/DefaultThreadPoolProvider.java | 34 ++++++++++++++ .../logRecord/thread/LogRecordThreadPool.java | 23 +++++---- .../logRecord/thread/ThreadPoolProvider.java | 16 +++++++ .../OperationLogCustomThreadPoolTest.java | 47 +++++++++++++++++++ .../service/CustomThreadPoolProvider.java | 33 +++++++++++++ ...perationLogGetCustomThreadPoolService.java | 27 +++++++++++ .../springboot3/test/service/TestService.java | 8 ++++ .../resources/testCustomThreadPool.properties | 2 + .../OperationLogCustomThreadPoolTest.java | 47 +++++++++++++++++++ .../service/CustomThreadPoolProvider.java | 33 +++++++++++++ ...perationLogGetCustomThreadPoolService.java | 27 +++++++++++ .../springboot/test/service/TestService.java | 8 ++++ .../resources/testCustomThreadPool.properties | 2 + 13 files changed, 297 insertions(+), 10 deletions(-) create mode 100644 log-record-core/src/main/java/cn/monitor4all/logRecord/thread/DefaultThreadPoolProvider.java create mode 100644 log-record-core/src/main/java/cn/monitor4all/logRecord/thread/ThreadPoolProvider.java create mode 100644 log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/OperationLogCustomThreadPoolTest.java create mode 100644 log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java create mode 100644 log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/OperationLogGetCustomThreadPoolService.java create mode 100644 log-record-springboot3-starter/src/test/resources/testCustomThreadPool.properties create mode 100644 log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/OperationLogCustomThreadPoolTest.java create mode 100644 log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java create mode 100644 log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/OperationLogGetCustomThreadPoolService.java create mode 100644 log-record-starter/src/test/resources/testCustomThreadPool.properties diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/DefaultThreadPoolProvider.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/DefaultThreadPoolProvider.java new file mode 100644 index 0000000..0e85cd2 --- /dev/null +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/DefaultThreadPoolProvider.java @@ -0,0 +1,34 @@ +package cn.monitor4all.logRecord.thread; + +import cn.monitor4all.logRecord.configuration.LogRecordProperties; +import lombok.extern.slf4j.Slf4j; +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; + +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * 默认线程池提供者 + */ +@Slf4j +public class DefaultThreadPoolProvider implements ThreadPoolProvider { + + private final LogRecordProperties logRecordProperties; + private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("log-record-"); + + + public DefaultThreadPoolProvider(LogRecordProperties logRecordProperties) { + this.logRecordProperties = logRecordProperties; + } + + @Override + public ThreadPoolExecutor buildLogRecordThreadPool() { + log.info("LogRecordThreadPool init poolSize [{}]", logRecordProperties.getThreadPool().getPoolSize()); + int poolSize = logRecordProperties.getThreadPool().getPoolSize(); + return new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); + } + + +} diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java index 22d8d11..2e27602 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java @@ -4,10 +4,10 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.scheduling.concurrent.CustomizableThreadFactory; +import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Component; -import java.util.concurrent.*; +import java.util.concurrent.ThreadPoolExecutor; @Slf4j @Component @@ -15,17 +15,20 @@ @EnableConfigurationProperties({LogRecordProperties.class}) public class LogRecordThreadPool { - private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("log-record-"); + private final ThreadPoolExecutor logRecordPoolExecutor; - private final ExecutorService LOG_RECORD_POOL_EXECUTOR; - public LogRecordThreadPool(LogRecordProperties logRecordProperties) { - log.info("LogRecordThreadPool init poolSize [{}]", logRecordProperties.getThreadPool().getPoolSize()); - int poolSize = logRecordProperties.getThreadPool().getPoolSize(); - this.LOG_RECORD_POOL_EXECUTOR = new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); + /** + * 操作日志主逻辑线程池 + * 提供顺序:用户传入线程池 > 通过配置文件创建的默认线程池 + */ + public LogRecordThreadPool(LogRecordProperties logRecordProperties, ApplicationContext applicationContext) { + ThreadPoolProvider threadPoolProvider = applicationContext.getBeanProvider(ThreadPoolProvider.class) + .getIfUnique(() -> new DefaultThreadPoolProvider(logRecordProperties)); + this.logRecordPoolExecutor = threadPoolProvider.buildLogRecordThreadPool(); } - public ExecutorService getLogRecordPoolExecutor() { - return LOG_RECORD_POOL_EXECUTOR; + public ThreadPoolExecutor getLogRecordPoolExecutor() { + return logRecordPoolExecutor; } } diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/ThreadPoolProvider.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/ThreadPoolProvider.java new file mode 100644 index 0000000..398414b --- /dev/null +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/ThreadPoolProvider.java @@ -0,0 +1,16 @@ +package cn.monitor4all.logRecord.thread; + + +import java.util.concurrent.ThreadPoolExecutor; + +/** + * 线程池提供者 + */ +public interface ThreadPoolProvider { + + /** + * 提供操作日志处理线程池 + */ + ThreadPoolExecutor buildLogRecordThreadPool(); + +} diff --git a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/OperationLogCustomThreadPoolTest.java b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/OperationLogCustomThreadPoolTest.java new file mode 100644 index 0000000..bb41c74 --- /dev/null +++ b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/OperationLogCustomThreadPoolTest.java @@ -0,0 +1,47 @@ +package cn.monitor4all.logRecord.springboot3.test; + + +import cn.monitor4all.logRecord.bean.LogDTO; +import cn.monitor4all.logRecord.springboot3.test.service.OperatorIdGetService; +import cn.monitor4all.logRecord.springboot3.test.service.TestService; +import cn.monitor4all.logRecord.springboot3.LogRecordAutoConfiguration; +import cn.monitor4all.logRecord.springboot3.test.utils.TestHelper; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.context.annotation.PropertySource; +import org.springframework.test.context.ContextConfiguration; + +/** + * 单元测试:自定义线程池 + */ +@Slf4j +@SpringBootTest +@ContextConfiguration(classes = { + LogRecordAutoConfiguration.class, + OperatorIdGetService.class, + TestService.class,}) +@PropertySource("classpath:testCustomThreadPool.properties") +@EnableAspectJAutoProxy(proxyTargetClass = true) +public class OperationLogCustomThreadPoolTest { + + @Autowired + private TestService testService; + + /** + * 测试:用户传入自定义线程池 + */ + @Test + public void testCustomThreadPool() { + TestHelper.addLock("testCustomThreadPool"); + testService.testCustomThreadPool(); + TestHelper.await("testCustomThreadPool"); + LogDTO logDTO = TestHelper.getLogDTO("testCustomThreadPool"); + + Assertions.assertEquals(logDTO.getBizType(), "testCustomThreadPool"); + } + +} diff --git a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java new file mode 100644 index 0000000..643a48e --- /dev/null +++ b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java @@ -0,0 +1,33 @@ +package cn.monitor4all.logRecord.springboot3.test.service; + +import cn.monitor4all.logRecord.thread.ThreadPoolProvider; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.test.context.TestComponent; +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; + +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +@Slf4j +@TestComponent +@ConditionalOnProperty(name = "test.config", havingValue = "customThreadPool") +public class CustomThreadPoolProvider implements ThreadPoolProvider { + + private static ThreadPoolExecutor EXECUTOR; + + private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("log-record-"); + + + private CustomThreadPoolProvider() { + log.info("CustomThreadPoolProvider init"); + EXECUTOR = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); + } + + @Override + public ThreadPoolExecutor buildLogRecordThreadPool() { + return EXECUTOR; + } +} diff --git a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/OperationLogGetCustomThreadPoolService.java b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/OperationLogGetCustomThreadPoolService.java new file mode 100644 index 0000000..7b77277 --- /dev/null +++ b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/OperationLogGetCustomThreadPoolService.java @@ -0,0 +1,27 @@ +package cn.monitor4all.logRecord.springboot3.test.service; + +import cn.monitor4all.logRecord.bean.LogDTO; +import cn.monitor4all.logRecord.service.IOperationLogGetService; +import cn.monitor4all.logRecord.springboot3.test.utils.TestHelper; +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.test.context.TestComponent; + +@Slf4j +@TestComponent +@ConditionalOnProperty(name = "test.config", havingValue = "customThreadPool") +public class OperationLogGetCustomThreadPoolService implements IOperationLogGetService { + + @Override + public boolean createLog(LogDTO logDTO) throws Exception { + log.info("logDTO: [{}]", JSON.toJSONString(logDTO)); + + if ("testCustomThreadPool".equals(logDTO.getBizType())) { + TestHelper.putLogDTO("testCustomThreadPool", logDTO); + TestHelper.releaseLock("testCustomThreadPool"); + } + + return true; + } +} diff --git a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/TestService.java b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/TestService.java index 9d4e23f..afaabda 100644 --- a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/TestService.java +++ b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/TestService.java @@ -237,4 +237,12 @@ public void testLogRecordContextTransmittableThreadLocal() { @OperationLog(bizId = "'1'", bizType = "'testRetryTimesAndOperationLogGetErrorHandler'") public void testRetryTimesAndOperationLogGetErrorHandler() { } + + @OperationLog(bizId = "'1'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'2'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'3'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'4'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'5'", bizType = "'testCustomThreadPool'") + public void testCustomThreadPool() { + } } diff --git a/log-record-springboot3-starter/src/test/resources/testCustomThreadPool.properties b/log-record-springboot3-starter/src/test/resources/testCustomThreadPool.properties new file mode 100644 index 0000000..5643072 --- /dev/null +++ b/log-record-springboot3-starter/src/test/resources/testCustomThreadPool.properties @@ -0,0 +1,2 @@ +test.config=customThreadPool +log-record.thread-pool.pool-size=10 \ No newline at end of file diff --git a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/OperationLogCustomThreadPoolTest.java b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/OperationLogCustomThreadPoolTest.java new file mode 100644 index 0000000..8a1a8f5 --- /dev/null +++ b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/OperationLogCustomThreadPoolTest.java @@ -0,0 +1,47 @@ +package cn.monitor4all.logRecord.springboot.test; + + +import cn.monitor4all.logRecord.bean.LogDTO; +import cn.monitor4all.logRecord.springboot.LogRecordAutoConfiguration; +import cn.monitor4all.logRecord.springboot.test.service.OperatorIdGetService; +import cn.monitor4all.logRecord.springboot.test.service.TestService; +import cn.monitor4all.logRecord.springboot.test.utils.TestHelper; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.context.annotation.PropertySource; +import org.springframework.test.context.ContextConfiguration; + +/** + * 单元测试:自定义线程池 + */ +@Slf4j +@SpringBootTest +@ContextConfiguration(classes = { + LogRecordAutoConfiguration.class, + OperatorIdGetService.class, + TestService.class,}) +@PropertySource("classpath:testCustomThreadPool.properties") +@EnableAspectJAutoProxy(proxyTargetClass = true) +public class OperationLogCustomThreadPoolTest { + + @Autowired + private TestService testService; + + /** + * 测试:用户传入自定义线程池 + */ + @Test + public void testCustomThreadPool() { + TestHelper.addLock("testCustomThreadPool"); + testService.testCustomThreadPool(); + TestHelper.await("testCustomThreadPool"); + LogDTO logDTO = TestHelper.getLogDTO("testCustomThreadPool"); + + Assertions.assertEquals(logDTO.getBizType(), "testCustomThreadPool"); + } + +} diff --git a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java new file mode 100644 index 0000000..740033e --- /dev/null +++ b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java @@ -0,0 +1,33 @@ +package cn.monitor4all.logRecord.springboot.test.service; + +import cn.monitor4all.logRecord.thread.ThreadPoolProvider; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.test.context.TestComponent; +import org.springframework.scheduling.concurrent.CustomizableThreadFactory; + +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +@Slf4j +@TestComponent +@ConditionalOnProperty(name = "test.config", havingValue = "customThreadPool") +public class CustomThreadPoolProvider implements ThreadPoolProvider { + + private static ThreadPoolExecutor EXECUTOR; + + private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("log-record-"); + + + private CustomThreadPoolProvider() { + log.info("CustomThreadPoolProvider init"); + EXECUTOR = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); + } + + @Override + public ThreadPoolExecutor buildLogRecordThreadPool() { + return EXECUTOR; + } +} diff --git a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/OperationLogGetCustomThreadPoolService.java b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/OperationLogGetCustomThreadPoolService.java new file mode 100644 index 0000000..e79b4ae --- /dev/null +++ b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/OperationLogGetCustomThreadPoolService.java @@ -0,0 +1,27 @@ +package cn.monitor4all.logRecord.springboot.test.service; + +import cn.monitor4all.logRecord.bean.LogDTO; +import cn.monitor4all.logRecord.service.IOperationLogGetService; +import cn.monitor4all.logRecord.springboot.test.utils.TestHelper; +import com.alibaba.fastjson.JSON; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.test.context.TestComponent; + +@Slf4j +@TestComponent +@ConditionalOnProperty(name = "test.config", havingValue = "customThreadPool") +public class OperationLogGetCustomThreadPoolService implements IOperationLogGetService { + + @Override + public boolean createLog(LogDTO logDTO) throws Exception { + log.info("logDTO: [{}]", JSON.toJSONString(logDTO)); + + if ("testCustomThreadPool".equals(logDTO.getBizType())) { + TestHelper.putLogDTO("testCustomThreadPool", logDTO); + TestHelper.releaseLock("testCustomThreadPool"); + } + + return true; + } +} diff --git a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/TestService.java b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/TestService.java index f938391..8b16e02 100644 --- a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/TestService.java +++ b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/TestService.java @@ -238,4 +238,12 @@ public void testLogRecordContextTransmittableThreadLocal() { @OperationLog(bizId = "'1'", bizType = "'testRetryTimesAndOperationLogGetErrorHandler'") public void testRetryTimesAndOperationLogGetErrorHandler() { } + + @OperationLog(bizId = "'1'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'2'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'3'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'4'", bizType = "'testCustomThreadPool'") + @OperationLog(bizId = "'5'", bizType = "'testCustomThreadPool'") + public void testCustomThreadPool() { + } } diff --git a/log-record-starter/src/test/resources/testCustomThreadPool.properties b/log-record-starter/src/test/resources/testCustomThreadPool.properties new file mode 100644 index 0000000..5643072 --- /dev/null +++ b/log-record-starter/src/test/resources/testCustomThreadPool.properties @@ -0,0 +1,2 @@ +test.config=customThreadPool +log-record.thread-pool.pool-size=10 \ No newline at end of file From cd8e56563720157f7ba9332afbe01f0dee542639 Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Sat, 25 May 2024 23:04:02 +0800 Subject: [PATCH 08/10] test: test custom thread name fix --- .../springboot3/test/service/CustomThreadPoolProvider.java | 2 +- .../springboot/test/service/CustomThreadPoolProvider.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java index 643a48e..aa1e85b 100644 --- a/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java +++ b/log-record-springboot3-starter/src/test/java/cn/monitor4all/logRecord/springboot3/test/service/CustomThreadPoolProvider.java @@ -18,7 +18,7 @@ public class CustomThreadPoolProvider implements ThreadPoolProvider { private static ThreadPoolExecutor EXECUTOR; - private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("log-record-"); + private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("custom-log-record-"); private CustomThreadPoolProvider() { diff --git a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java index 740033e..42efe36 100644 --- a/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java +++ b/log-record-starter/src/test/java/cn/monitor4all/logRecord/springboot/test/service/CustomThreadPoolProvider.java @@ -18,7 +18,7 @@ public class CustomThreadPoolProvider implements ThreadPoolProvider { private static ThreadPoolExecutor EXECUTOR; - private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("log-record-"); + private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("custom-log-record-"); private CustomThreadPoolProvider() { From 2c6890b150a983661d2f252a7362d2d1d07330ff Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Sun, 26 May 2024 15:57:04 +0800 Subject: [PATCH 09/10] docs: update README --- README.md | 52 +++++++++++++++---- .../logRecord/thread/LogRecordThreadPool.java | 2 +- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 9e31f71..24b161a 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,8 @@ public Response function(Request request) { - 支持自动重试和兜底处理:支持配置重试次数和处理失败兜底逻辑`SPI` - 支持控制切面执行时机(方法执行前后) - 支持自定义执行成功判断 +- 支持非注解方式手动记录日志 +- 自定义消息线程池 - 更多特性等你来发掘... **日志实体(LogDTO)内包含:** @@ -347,8 +349,9 @@ public Response function(Request request) { - [实体类`Diff`](#实体类Diff) - [日志处理重试次数及兜底函数配置](#日志处理重试次数及兜底函数配置) - [重复注解](#重复注解) -- [消息分发线程池配置](#消息分发线程池配置) +- [自定义消息线程池](#自定义消息线程池) - [函数返回值记录开关](#函数返回值记录开关) +- [非注解方式手动记录日志](#非注解方式) - [操作日志数据表结构推荐](#操作日志数据表结构推荐) - [让注解支持`IDEA`自动补全](#让注解支持IDEA自动补全) @@ -512,7 +515,7 @@ public Response function(Request request) { } ``` -LogRecordContext内部使用TransmittableThreadLocal,在线程池中也可以读取到主线程的ThreadLocal。 +LogRecordContext内部使用TransmittableThreadLocal实现与主线程的ThreadLocal传递。 ### 自定义函数 @@ -795,20 +798,49 @@ public class LogRecordErrorHandlerServiceImpl implements LogRecordErrorHandlerSe 我们还加上了重复注解的支持,可以在一个方法上同时加多个`@OperationLog`,**会保证按照`@OperationLog`从上到下的顺序输出日志**。 -### 消息分发线程池配置 +### 自定义消息线程池 -在组装好`logDTO`后,默认使用线程池对消息进行分发,发送至本地监听函数或者消息队列发送者。 - -**注意:`logDTO`的组装在切面中,该切面仍然在函数执行的线程中运行。** - -可以使用如下配置: +starter提供了如下配置: ```properties log-record.thread-pool.pool-size=4(线程池核心线程大小 默认为4) -log-record.thread-pool.enabled=true(线程池开关 默认为开启 若关闭则使用主线程进行消息处理发送) +log-record.thread-pool.enabled=true(线程池开关 默认为开启 若关闭则使用业务线程进行消息处理发送) +``` + +在组装好`logDTO`后,默认会使用线程池对消息进行处理,发送至本地监听函数或者消息队列发送者,也可以通过配置关闭线程池,让主线程执行全部消息处理逻辑。 + +**注意:`logDTO`的组装逻辑在切面中,该切面仍然在函数执行的线程中运行。** + +默认线程池配置如下(拒绝策略为丢弃): + +```java +return new ThreadPoolExecutor(poolSize, poolSize, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(1024), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); +``` + +此外,还提供了用户传入自定义线程池的方式,用户可自行实现cn.monitor4all.logRecord.thread.ThreadPoolProvider,传入线程池。 + +示例: + +```java +public class CustomThreadPoolProvider implements ThreadPoolProvider { + + private static ThreadPoolExecutor EXECUTOR; + + private static final ThreadFactory THREAD_FACTORY = new CustomizableThreadFactory("custom-log-record-"); + + + private CustomThreadPoolProvider() { + log.info("CustomThreadPoolProvider init"); + EXECUTOR = new ThreadPoolExecutor(3, 3, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(100), THREAD_FACTORY, new ThreadPoolExecutor.AbortPolicy()); + } + + @Override + public ThreadPoolExecutor buildLogRecordThreadPool() { + return EXECUTOR; + } +} ``` -关闭使用线程池后,所有发送由主线程执行,带来的副作用是大量日志并发发送,会降低主线程处理效率。 ### 函数返回值记录开关 diff --git a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java index 2e27602..d624564 100644 --- a/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java +++ b/log-record-core/src/main/java/cn/monitor4all/logRecord/thread/LogRecordThreadPool.java @@ -20,7 +20,7 @@ public class LogRecordThreadPool { /** * 操作日志主逻辑线程池 - * 提供顺序:用户传入线程池 > 通过配置文件创建的默认线程池 + * 提供顺序:用户传入线程池 优先于 通过配置文件创建的默认线程池 */ public LogRecordThreadPool(LogRecordProperties logRecordProperties, ApplicationContext applicationContext) { ThreadPoolProvider threadPoolProvider = applicationContext.getBeanProvider(ThreadPoolProvider.class) From deb9dd7c9c23f04ba6ef6d24d001532872d979c3 Mon Sep 17 00:00:00 2001 From: Zhendong Yang Date: Sun, 26 May 2024 16:20:05 +0800 Subject: [PATCH 10/10] version: 1.6.3 --- log-record-core/pom.xml | 2 +- log-record-springboot3-starter/pom.xml | 4 ++-- log-record-starter/pom.xml | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/log-record-core/pom.xml b/log-record-core/pom.xml index 9aa1a66..4852428 100644 --- a/log-record-core/pom.xml +++ b/log-record-core/pom.xml @@ -10,7 +10,7 @@ cn.monitor4all log-record-core - 1.6.3-SNAPSHOT + 1.6.3 8 diff --git a/log-record-springboot3-starter/pom.xml b/log-record-springboot3-starter/pom.xml index 876abfc..a7e701b 100644 --- a/log-record-springboot3-starter/pom.xml +++ b/log-record-springboot3-starter/pom.xml @@ -10,7 +10,7 @@ cn.monitor4all log-record-springboot3-starter - 1.6.3-SNAPSHOT + 1.6.3 17 @@ -35,7 +35,7 @@ cn.monitor4all log-record-core - 1.6.3-SNAPSHOT + 1.6.3 diff --git a/log-record-starter/pom.xml b/log-record-starter/pom.xml index cb904c9..28b9e43 100644 --- a/log-record-starter/pom.xml +++ b/log-record-starter/pom.xml @@ -10,7 +10,7 @@ cn.monitor4all log-record-starter - 1.6.3-SNAPSHOT + 1.6.3 8 @@ -35,7 +35,7 @@ cn.monitor4all log-record-core - 1.6.3-SNAPSHOT + 1.6.3