Skip to content

Commit 2431767

Browse files
committed
Register the LoggingSystem in the ApplicationContext
Closes gh-4159
1 parent 05a2b53 commit 2431767

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
2626

27+
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2728
import org.springframework.boot.ApplicationPid;
2829
import org.springframework.boot.SpringApplication;
2930
import org.springframework.boot.bind.RelaxedPropertyResolver;
3031
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
32+
import org.springframework.boot.context.event.ApplicationPreparedEvent;
3133
import org.springframework.boot.context.event.ApplicationStartedEvent;
3234
import org.springframework.context.ApplicationContext;
3335
import org.springframework.context.ApplicationEvent;
@@ -107,6 +109,11 @@ public class LoggingApplicationListener implements GenericApplicationListener {
107109
*/
108110
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD";
109111

112+
/**
113+
* The name of the {@link LoggingSystem} bean.
114+
*/
115+
private static final String LOGGING_SYSTEM_BEAN_NAME = "springBootLoggingSystem";
116+
110117
private static MultiValueMap<LogLevel, String> LOG_LEVEL_LOGGERS;
111118

112119
private static AtomicBoolean shutdownHookRegistered = new AtomicBoolean(false);
@@ -123,7 +130,8 @@ public class LoggingApplicationListener implements GenericApplicationListener {
123130
}
124131

125132
private static Class<?>[] EVENT_TYPES = { ApplicationStartedEvent.class,
126-
ApplicationEnvironmentPreparedEvent.class, ContextClosedEvent.class };
133+
ApplicationEnvironmentPreparedEvent.class, ApplicationPreparedEvent.class,
134+
ContextClosedEvent.class };
127135

128136
private static Class<?>[] SOURCE_TYPES = { SpringApplication.class,
129137
ApplicationContext.class };
@@ -168,6 +176,9 @@ else if (event instanceof ApplicationEnvironmentPreparedEvent) {
168176
onApplicationEnvironmentPreparedEvent(
169177
(ApplicationEnvironmentPreparedEvent) event);
170178
}
179+
else if (event instanceof ApplicationPreparedEvent) {
180+
onApplicationPreparedEvent((ApplicationPreparedEvent) event);
181+
}
171182
else if (event instanceof ContextClosedEvent) {
172183
onContextClosedEvent();
173184
}
@@ -188,6 +199,13 @@ private void onApplicationEnvironmentPreparedEvent(
188199
initialize(event.getEnvironment(), event.getSpringApplication().getClassLoader());
189200
}
190201

202+
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
203+
ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory();
204+
if (!beanFactory.containsBean(LOGGING_SYSTEM_BEAN_NAME)) {
205+
beanFactory.registerSingleton(LOGGING_SYSTEM_BEAN_NAME, this.loggingSystem);
206+
}
207+
}
208+
191209
private void onContextClosedEvent() {
192210
if (this.loggingSystem != null) {
193211
this.loggingSystem.cleanUp();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2012-2015 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.logging;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.builder.SpringApplicationBuilder;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
import org.springframework.stereotype.Component;
25+
26+
import static org.junit.Assert.assertNotNull;
27+
28+
/**
29+
* Integration tests for {@link LoggingApplicationListener}.
30+
*
31+
* @author Stephane Nicoll
32+
*/
33+
public class LoggingApplicationListenerIntegrationTests {
34+
35+
36+
@Test
37+
public void loggingSystemRegisteredInTheContext() {
38+
ConfigurableApplicationContext context = new SpringApplicationBuilder(
39+
SampleService.class).web(false).run();
40+
try {
41+
SampleService service = context.getBean(SampleService.class);
42+
assertNotNull(service.loggingSystem);
43+
}
44+
finally {
45+
context.close();
46+
}
47+
}
48+
49+
50+
@Component
51+
static class SampleService {
52+
53+
private final LoggingSystem loggingSystem;
54+
55+
@Autowired
56+
SampleService(LoggingSystem loggingSystem) {
57+
this.loggingSystem = loggingSystem;
58+
}
59+
}
60+
61+
}
62+

0 commit comments

Comments
 (0)