Add support for rebinding log levels

This commit is contained in:
Dave Syer
2014-07-08 13:07:44 +01:00
parent f8497e62c7
commit 5d230d7564
5 changed files with 117 additions and 2 deletions

View File

@@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.1.4.BUILD-SNAPSHOT</version>
<version>1.1.5.BUILD-SNAPSHOT</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modules>

View File

@@ -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) {

View File

@@ -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<EnvironmentChangeEvent>, 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);
}
}

View File

@@ -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());
}
}

View File

@@ -43,7 +43,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>org.springframework.platform.config.server.Application</start-class>
<start-class>sample.Application</start-class>
<java.version>1.7</java.version>
</properties>