From 5d230d7564d6d40e8fe2f2d494ce5414d7949df3 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 8 Jul 2014 13:07:44 +0100 Subject: [PATCH] Add support for rebinding log levels --- pom.xml | 2 +- .../RefreshAutoConfiguration.java | 7 +++ .../platform/logging/LoggingRebinder.java | 51 +++++++++++++++++ .../logging/LoggingRebinderTests.java | 57 +++++++++++++++++++ spring-platform-config-sample/pom.xml | 2 +- 5 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 spring-platform-config-client/src/main/java/org/springframework/platform/logging/LoggingRebinder.java create mode 100644 spring-platform-config-client/src/test/java/org/springframework/platform/logging/LoggingRebinderTests.java diff --git a/pom.xml b/pom.xml index ea5338f7..a523c354 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ org.springframework.boot spring-boot-starter-parent - 1.1.4.BUILD-SNAPSHOT + 1.1.5.BUILD-SNAPSHOT diff --git a/spring-platform-config-client/src/main/java/org/springframework/platform/autoconfigure/RefreshAutoConfiguration.java b/spring-platform-config-client/src/main/java/org/springframework/platform/autoconfigure/RefreshAutoConfiguration.java index e582d63c..db793b97 100644 --- a/spring-platform-config-client/src/main/java/org/springframework/platform/autoconfigure/RefreshAutoConfiguration.java +++ b/spring-platform-config-client/src/main/java/org/springframework/platform/autoconfigure/RefreshAutoConfiguration.java @@ -46,6 +46,7 @@ import org.springframework.platform.context.restart.RestartEndpoint; import org.springframework.platform.context.restart.RestartMvcEndpoint; import org.springframework.platform.context.scope.refresh.RefreshScope; import org.springframework.platform.endpoint.GenericPostableMvcEndpoint; +import org.springframework.platform.logging.LoggingRebinder; @Configuration @ConditionalOnClass(RefreshScope.class) @@ -58,6 +59,12 @@ public class RefreshAutoConfiguration { return new RefreshScope(); } + @Bean + @ConditionalOnMissingBean + public static LoggingRebinder loggingRebinder() { + return new LoggingRebinder(); + } + @Bean @ConditionalOnMissingBean public EnvironmentManager environmentManager(ConfigurableEnvironment environment) { diff --git a/spring-platform-config-client/src/main/java/org/springframework/platform/logging/LoggingRebinder.java b/spring-platform-config-client/src/main/java/org/springframework/platform/logging/LoggingRebinder.java new file mode 100644 index 00000000..a5cce5b3 --- /dev/null +++ b/spring-platform-config-client/src/main/java/org/springframework/platform/logging/LoggingRebinder.java @@ -0,0 +1,51 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.platform.logging; + +import org.springframework.boot.logging.LoggingApplicationListener; +import org.springframework.boot.logging.LoggingSystem; +import org.springframework.context.ApplicationListener; +import org.springframework.context.EnvironmentAware; +import org.springframework.core.env.Environment; +import org.springframework.platform.context.environment.EnvironmentChangeEvent; + +/** + * @author Dave Syer + * + */ +public class LoggingRebinder implements + ApplicationListener, EnvironmentAware { + + private Environment environment; + + private LoggingApplicationListener delegate = new LoggingApplicationListener(); + + @Override + public void setEnvironment(Environment environment) { + this.environment = environment; + } + + @Override + public void onApplicationEvent(EnvironmentChangeEvent event) { + if (environment == null) { + return; + } + LoggingSystem system = LoggingSystem.get(LoggingSystem.class + .getClassLoader()); + delegate.setLogLevels(system, environment); + } + +} diff --git a/spring-platform-config-client/src/test/java/org/springframework/platform/logging/LoggingRebinderTests.java b/spring-platform-config-client/src/test/java/org/springframework/platform/logging/LoggingRebinderTests.java new file mode 100644 index 00000000..6cfcd5d1 --- /dev/null +++ b/spring-platform-config-client/src/test/java/org/springframework/platform/logging/LoggingRebinderTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 2013-2014 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.platform.logging; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.Collections; + +import org.junit.After; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.logging.LogLevel; +import org.springframework.boot.logging.LoggingSystem; +import org.springframework.boot.test.EnvironmentTestUtils; +import org.springframework.core.env.StandardEnvironment; +import org.springframework.platform.context.environment.EnvironmentChangeEvent; + +/** + * @author Dave Syer + * + */ +public class LoggingRebinderTests { + + private LoggingRebinder rebinder = new LoggingRebinder(); + private Logger logger = LoggerFactory.getLogger("org.springframework.web"); + + @After + public void reset() { + LoggingSystem.get(getClass().getClassLoader()).setLogLevel("org.springframework.web", LogLevel.INFO); + } + + @Test + public void logLevelsChanged() { + assertFalse(logger.isTraceEnabled()); + StandardEnvironment environment = new StandardEnvironment(); + EnvironmentTestUtils.addEnvironment(environment, "logging.level.org.springframework.web=TRACE"); + rebinder.setEnvironment(environment); + rebinder.onApplicationEvent(new EnvironmentChangeEvent(Collections.singleton("logging.level.org.springframework.web"))); + assertTrue(logger.isTraceEnabled()); + } + +} diff --git a/spring-platform-config-sample/pom.xml b/spring-platform-config-sample/pom.xml index 93cf3b0e..b9b7bd77 100644 --- a/spring-platform-config-sample/pom.xml +++ b/spring-platform-config-sample/pom.xml @@ -43,7 +43,7 @@ UTF-8 - org.springframework.platform.config.server.Application + sample.Application 1.7