diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index af4924d3d2..ca4fb37b3c 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -24,10 +24,12 @@ import java.util.concurrent.atomic.AtomicBoolean; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.boot.ApplicationPid; import org.springframework.boot.SpringApplication; import org.springframework.boot.bind.RelaxedPropertyResolver; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; +import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.context.ApplicationContext; 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"; + /** + * The name of the {@link LoggingSystem} bean. + */ + private static final String LOGGING_SYSTEM_BEAN_NAME = "springBootLoggingSystem"; + private static MultiValueMap LOG_LEVEL_LOGGERS; private static AtomicBoolean shutdownHookRegistered = new AtomicBoolean(false); @@ -123,7 +130,8 @@ public class LoggingApplicationListener implements GenericApplicationListener { } private static Class[] EVENT_TYPES = { ApplicationStartedEvent.class, - ApplicationEnvironmentPreparedEvent.class, ContextClosedEvent.class }; + ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class, + ContextClosedEvent.class }; private static Class[] SOURCE_TYPES = { SpringApplication.class, ApplicationContext.class }; @@ -168,6 +176,9 @@ public class LoggingApplicationListener implements GenericApplicationListener { onApplicationEnvironmentPreparedEvent( (ApplicationEnvironmentPreparedEvent) event); } + else if (event instanceof ApplicationPreparedEvent) { + onApplicationPreparedEvent((ApplicationPreparedEvent) event); + } else if (event instanceof ContextClosedEvent) { onContextClosedEvent(); } @@ -188,6 +199,13 @@ public class LoggingApplicationListener implements GenericApplicationListener { 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() { if (this.loggingSystem != null) { this.loggingSystem.cleanUp(); diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java new file mode 100644 index 0000000000..123c18cc69 --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerIntegrationTests.java @@ -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; + } + } + +} +