Fix not able to override datasource-proxy log-level (#1976)
* Fix datasource-proxy log-level is not working - Aligned default values of `Query` & `SlowQuery` in `TraceJdbcProperties.DataSourceProxyProperties` and `DataSourceProxyProperties` - Add code to copy properties of `Query` and `SlowQuery` in `DataSourceProxyConfiguration` - Add test case to check if `log-level` can be override or not. Fixes gh-1973 * Disable query log listener by default - By default `QueryLogListener` and `LoggingQueryLogListener` will be disabled, dev need to enable it by using properties - By default `Slf4j` will be enlabled for query logging so by adding below property slow query logging will be enabled `spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true` this property will register `SLF4JSlowQueryListener` * Add docs for how-to enable data-proxy query logs
This commit is contained in:
@@ -50,6 +50,7 @@ import org.springframework.context.annotation.Bean;
|
||||
* {@link QueryTransformer}.
|
||||
*
|
||||
* @author Arthur Gavlyukovskiy
|
||||
* @author Chintan Radia
|
||||
*/
|
||||
@ConditionalOnClass(ProxyDataSource.class)
|
||||
@ConditionalOnProperty(name = "spring.sleuth.jdbc.datasource-proxy.enabled", havingValue = "true",
|
||||
@@ -84,6 +85,8 @@ class DataSourceProxyConfiguration {
|
||||
DataSourceProxyProperties props = new DataSourceProxyProperties();
|
||||
BeanUtils.copyProperties(originalProxy, props);
|
||||
props.setLogging(DataSourceProxyProperties.DataSourceProxyLogging.valueOf(originalProxy.getLogging().name()));
|
||||
BeanUtils.copyProperties(originalProxy.getQuery(), props.getQuery());
|
||||
BeanUtils.copyProperties(originalProxy.getSlowQuery(), props.getSlowQuery());
|
||||
return props;
|
||||
}
|
||||
|
||||
|
||||
@@ -253,7 +253,7 @@ public class TraceJdbcProperties {
|
||||
*/
|
||||
private long threshold = 300;
|
||||
|
||||
boolean isEnableLogging() {
|
||||
public boolean isEnableLogging() {
|
||||
return enableLogging;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ 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.SLF4JLogLevel;
|
||||
import net.ttddyy.dsproxy.listener.logging.SLF4JQueryLoggingListener;
|
||||
import net.ttddyy.dsproxy.listener.logging.SLF4JSlowQueryListener;
|
||||
import net.ttddyy.dsproxy.listener.logging.SystemOutQueryLoggingListener;
|
||||
@@ -53,6 +54,7 @@ import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.filter;
|
||||
|
||||
class ProxyDataSourceConfigurationTests {
|
||||
|
||||
@@ -65,7 +67,24 @@ class ProxyDataSourceConfigurationTests {
|
||||
.withClassLoader(new FilteredClassLoader("com.p6spy"));
|
||||
|
||||
@Test
|
||||
void testRegisterLogAndSlowQueryLogByDefaultToSlf4j() {
|
||||
void testNotRegisterLogAndSlowQueryLogByDefaultToSlf4j() {
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
ProxyDataSource proxyDataSource = (ProxyDataSource) ((DataSourceWrapper) dataSource)
|
||||
.getDecoratedDataSource();
|
||||
ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
|
||||
assertThat(chainListener.getListeners()).extracting("class").doesNotContain(SLF4JSlowQueryListener.class);
|
||||
assertThat(chainListener.getListeners()).extracting("class")
|
||||
.doesNotContain(SLF4JQueryLoggingListener.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testRegisterLogAndSlowQueryLogByDefaultToSlf4jWhenLoggingIsEnabled() {
|
||||
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
|
||||
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
ProxyDataSource proxyDataSource = (ProxyDataSource) ((DataSourceWrapper) dataSource)
|
||||
@@ -78,8 +97,10 @@ class ProxyDataSourceConfigurationTests {
|
||||
|
||||
@Test
|
||||
void testRegisterLogAndSlowQueryLogByUsingSlf4j() {
|
||||
ApplicationContextRunner contextRunner = this.contextRunner
|
||||
.withPropertyValues("spring.sleuth.jdbc.datasource-proxy.logging=slf4j");
|
||||
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
|
||||
"spring.sleuth.jdbc.datasource-proxy.logging=slf4j",
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
|
||||
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
@@ -93,8 +114,10 @@ class ProxyDataSourceConfigurationTests {
|
||||
|
||||
@Test
|
||||
void testRegisterLogAndSlowQueryLogUsingSystemOut() {
|
||||
ApplicationContextRunner contextRunner = this.contextRunner
|
||||
.withPropertyValues("spring.sleuth.jdbc.datasource-proxy.logging=sysout");
|
||||
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
|
||||
"spring.sleuth.jdbc.datasource-proxy.logging=sysout",
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
|
||||
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
@@ -108,8 +131,10 @@ class ProxyDataSourceConfigurationTests {
|
||||
|
||||
@Test
|
||||
void testRegisterLogAndSlowQueryLogUsingJUL() {
|
||||
ApplicationContextRunner contextRunner = this.contextRunner
|
||||
.withPropertyValues("spring.sleuth.jdbc.datasourceProxy.logging=jul");
|
||||
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
|
||||
"spring.sleuth.jdbc.datasourceProxy.logging=jul",
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
|
||||
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
@@ -123,8 +148,10 @@ class ProxyDataSourceConfigurationTests {
|
||||
|
||||
@Test
|
||||
void testRegisterLogAndSlowQueryLogUsingApacheCommons() {
|
||||
ApplicationContextRunner contextRunner = this.contextRunner
|
||||
.withPropertyValues("spring.sleuth.jdbc.datasourceProxy.logging=commons");
|
||||
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
|
||||
"spring.sleuth.jdbc.datasourceProxy.logging=commons",
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true");
|
||||
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
@@ -193,6 +220,27 @@ class ProxyDataSourceConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void testLogLevelIsCustomizableForLogAndSlowQueryInSlf4j() {
|
||||
ApplicationContextRunner contextRunner = this.contextRunner.withPropertyValues(
|
||||
"spring.sleuth.jdbc.datasource-proxy.logging=slf4j",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.enable-logging=true",
|
||||
"spring.sleuth.jdbc.datasource-proxy.slow-query.log-level=INFO",
|
||||
"spring.sleuth.jdbc.datasource-proxy.query.log-level=INFO");
|
||||
|
||||
contextRunner.run(context -> {
|
||||
DataSource dataSource = context.getBean(DataSource.class);
|
||||
ProxyDataSource proxyDataSource = (ProxyDataSource) ((DataSourceWrapper) dataSource)
|
||||
.getDecoratedDataSource();
|
||||
ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener();
|
||||
assertThat(filter(chainListener.getListeners()).with("class").equalsTo(SLF4JQueryLoggingListener.class)
|
||||
.with("logLevel").equalsTo(SLF4JLogLevel.INFO).get()).hasSize(1);
|
||||
assertThat(filter(chainListener.getListeners()).with("class").equalsTo(SLF4JSlowQueryListener.class)
|
||||
.with("logLevel").equalsTo(SLF4JLogLevel.INFO).get()).hasSize(1);
|
||||
});
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class CustomDataSourceProxyConfiguration {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user