From 04a4df7630d3040ca66a0d349da2849f9cc5e39b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frederik=20B=C3=BClthoff?= <232148+frederikb@users.noreply.github.com> Date: Mon, 14 Jul 2025 09:31:09 +0200 Subject: [PATCH] Add logging filter support for datasource-proxy --- README.md | 10 +++++++- .../ProxyDataSourceBuilderConfigurer.java | 8 +++++++ .../ProxyDataSourceConfigurationTests.java | 24 +++++++++++++++++++ gradle/libs.versions.toml | 2 +- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a995d3f..61c0d51 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ public ConnectionIdManagerProvider connectionIdManagerProvider() { ``` You can configure logging, query/slow query listeners and more using your `application.properties`: > [!NOTE] -> Configuration below indicates al possible parameters together with their default values and **does not** need to be set explicitly +> Configuration below indicates all possible parameters together with their default values and **does not** need to be set explicitly ```properties # One of logging libraries (slf4j, jul, common, sysout) decorator.datasource.datasource-proxy.logging=slf4j @@ -214,6 +214,14 @@ decorator.datasource.datasource-proxy.json-format=false decorator.datasource.datasource-proxy.count-query=false ``` +Optionally, configure a `LoggingFilter` to control the output of query logging: +```java +@Bean +public LoggingFilter loggingFilter() { + return MyLoggingFilter(); +} +``` + #### Flexy Pool If the `flexy-pool-spring-boot-starter` is added to the classpath your datasource will be wrapped to the `FlexyPoolDataSource`. diff --git a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceBuilderConfigurer.java b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceBuilderConfigurer.java index 2dd4471..25fc1e2 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceBuilderConfigurer.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/main/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceBuilderConfigurer.java @@ -20,6 +20,7 @@ import net.ttddyy.dsproxy.listener.QueryCountStrategy; import net.ttddyy.dsproxy.listener.QueryExecutionListener; import net.ttddyy.dsproxy.listener.logging.CommonsLogLevel; +import net.ttddyy.dsproxy.listener.logging.LoggingFilter; import net.ttddyy.dsproxy.listener.logging.SLF4JLogLevel; import net.ttddyy.dsproxy.proxy.ResultSetProxyLogicFactory; import net.ttddyy.dsproxy.support.ProxyDataSourceBuilder; @@ -70,6 +71,9 @@ public class ProxyDataSourceBuilderConfigurer { @Autowired(required = false) ProxyDataSourceBuilder.FormatQueryCallback formatQueryCallback; + @Autowired(required = false) + private LoggingFilter loggingFilter; + public void configure(ProxyDataSourceBuilder proxyDataSourceBuilder, DataSourceProxyProperties datasourceProxy) { var query = datasourceProxy.getQuery(); var slowQuery = datasourceProxy.getSlowQuery(); @@ -126,6 +130,10 @@ public void configure(ProxyDataSourceBuilder proxyDataSourceBuilder, DataSourceP log.warn("Found opposite format-sql and json format, json format will be used (may depend on library version)"); } + if (loggingFilter != null) { + proxyDataSourceBuilder.loggingFilter(loggingFilter); + } + if (datasourceProxy.isMultiline()) { proxyDataSourceBuilder.multiline(); } diff --git a/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceConfigurationTests.java b/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceConfigurationTests.java index bcb26d0..dcac3cb 100644 --- a/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceConfigurationTests.java +++ b/datasource-decorator-spring-boot-autoconfigure/src/test/java/com/github/gavlyukovskiy/boot/jdbc/decorator/dsproxy/ProxyDataSourceConfigurationTests.java @@ -23,10 +23,12 @@ import net.ttddyy.dsproxy.QueryInfo; import net.ttddyy.dsproxy.listener.ChainListener; import net.ttddyy.dsproxy.listener.QueryExecutionListener; +import net.ttddyy.dsproxy.listener.logging.AbstractQueryLoggingListener; import net.ttddyy.dsproxy.listener.logging.CommonsQueryLoggingListener; import net.ttddyy.dsproxy.listener.logging.CommonsSlowQueryListener; import net.ttddyy.dsproxy.listener.logging.JULQueryLoggingListener; import net.ttddyy.dsproxy.listener.logging.JULSlowQueryListener; +import net.ttddyy.dsproxy.listener.logging.LoggingFilter; import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener; import net.ttddyy.dsproxy.listener.logging.SLF4JSlowQueryListener; import net.ttddyy.dsproxy.listener.logging.SystemOutQueryLoggingListener; @@ -227,6 +229,19 @@ void testCustomConnectionIdManager() { }); } + @Test + void testCustomLoggingFilter() { + ApplicationContextRunner contextRunner = this.contextRunner.withUserConfiguration(CustomLoggingFilterConfiguration.class); + + contextRunner.run(context -> { + DataSource dataSource = context.getBean(DataSource.class); + ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); + LoggingFilter loggingFilter = context.getBean(LoggingFilter.class); + var queryListener = findListener(proxyDataSource, AbstractQueryLoggingListener.class); + assertThat(queryListener.getLoggingFilter()).isSameAs(loggingFilter); + }); + } + @Configuration(proxyBeanMethods = false) static class CustomDataSourceProxyConfiguration { @@ -264,4 +279,13 @@ public void afterQuery(ExecutionInfo execInfo, List queryInfoList) { }; } } + + @Configuration(proxyBeanMethods = false) + static class CustomLoggingFilterConfiguration { + + @Bean + public LoggingFilter loggingFilter() { + return (execInfo, queryInfoList) -> true; + } + } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 1c11f50..c7a5ad5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] spring-boot = "3.5.3" p6Spy = "3.9.1" -datasource-proxy = "1.10.1" +datasource-proxy = "1.11.0" flexy-pool = "3.0.2" [libraries]