Register the LoggingSystem in the ApplicationContext

Closes gh-4159
pull/4304/head
Stephane Nicoll 9 years ago
parent 05a2b53527
commit 2431767c81

@ -24,10 +24,12 @@ import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.ApplicationPid; import org.springframework.boot.ApplicationPid;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework.boot.context.event.ApplicationPreparedEvent;
import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
@ -107,6 +109,11 @@ public class LoggingApplicationListener implements GenericApplicationListener {
*/ */
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD"; public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
/**
* The name of the {@link LoggingSystem} bean.
*/
private static final String LOGGING_SYSTEM_BEAN_NAME = "springBootLoggingSystem";
private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS; private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS;
private static AtomicBoolean shutdownHookRegistered = new AtomicBoolean(false); private static AtomicBoolean shutdownHookRegistered = new AtomicBoolean(false);
@ -123,7 +130,8 @@ public class LoggingApplicationListener implements GenericApplicationListener {
} }
private static Class<?>[] EVENT_TYPES = { ApplicationStartedEvent.class, private static Class<?>[] EVENT_TYPES = { ApplicationStartedEvent.class,
ApplicationEnvironmentPreparedEvent.class, ContextClosedEvent.class }; ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,
ContextClosedEvent.class };
private static Class<?>[] SOURCE_TYPES = { SpringApplication.class, private static Class<?>[] SOURCE_TYPES = { SpringApplication.class,
ApplicationContext.class }; ApplicationContext.class };
@ -168,6 +176,9 @@ public class LoggingApplicationListener implements GenericApplicationListener {
onApplicationEnvironmentPreparedEvent( onApplicationEnvironmentPreparedEvent(
(ApplicationEnvironmentPreparedEvent) event); (ApplicationEnvironmentPreparedEvent) event);
} }
else if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
}
else if (event instanceof ContextClosedEvent) { else if (event instanceof ContextClosedEvent) {
onContextClosedEvent(); onContextClosedEvent();
} }
@ -188,6 +199,13 @@ public class LoggingApplicationListener implements GenericApplicationListener {
initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader()); initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
} }
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory();
if (!beanFactory.containsBean(LOGGING_SYSTEM_BEAN_NAME)) {
beanFactory.registerSingleton(LOGGING_SYSTEM_BEAN_NAME, this.loggingSystem);
}
}
private void onContextClosedEvent() { private void onContextClosedEvent() {
if (this.loggingSystem != null) { if (this.loggingSystem != null) {
this.loggingSystem.cleanUp(); this.loggingSystem.cleanUp();

@ -0,0 +1,62 @@
/*
* Copyright 2012-2015 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.boot.logging;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import static org.junit.Assert.assertNotNull;
/**
* Integration tests for {@link LoggingApplicationListener}.
*
* @author Stephane Nicoll
*/
public class LoggingApplicationListenerIntegrationTests {
@Test
public void loggingSystemRegisteredInTheContext() {
ConfigurableApplicationContext context = new SpringApplicationBuilder(
SampleService.class).web(false).run();
try {
SampleService service = context.getBean(SampleService.class);
assertNotNull(service.loggingSystem);
}
finally {
context.close();
}
}
@Component
static class SampleService {
private final LoggingSystem loggingSystem;
@Autowired
SampleService(LoggingSystem loggingSystem) {
this.loggingSystem = loggingSystem;
}
}
}
Loading…
Cancel
Save