24
24
import org .apache .commons .logging .Log ;
25
25
import org .apache .commons .logging .LogFactory ;
26
26
27
+ import org .springframework .beans .factory .config .ConfigurableListableBeanFactory ;
27
28
import org .springframework .boot .ApplicationPid ;
28
29
import org .springframework .boot .SpringApplication ;
29
30
import org .springframework .boot .bind .RelaxedPropertyResolver ;
30
31
import org .springframework .boot .context .event .ApplicationEnvironmentPreparedEvent ;
32
+ import org .springframework .boot .context .event .ApplicationPreparedEvent ;
31
33
import org .springframework .boot .context .event .ApplicationStartedEvent ;
32
34
import org .springframework .context .ApplicationContext ;
33
35
import org .springframework .context .ApplicationEvent ;
@@ -107,6 +109,11 @@ public class LoggingApplicationListener implements GenericApplicationListener {
107
109
*/
108
110
public static final String EXCEPTION_CONVERSION_WORD = "LOG_EXCEPTION_CONVERSION_WORD" ;
109
111
112
+ /**
113
+ * The name of the {@link LoggingSystem} bean.
114
+ */
115
+ private static final String LOGGING_SYSTEM_BEAN_NAME = "springBootLoggingSystem" ;
116
+
110
117
private static MultiValueMap <LogLevel , String > LOG_LEVEL_LOGGERS ;
111
118
112
119
private static AtomicBoolean shutdownHookRegistered = new AtomicBoolean (false );
@@ -123,7 +130,8 @@ public class LoggingApplicationListener implements GenericApplicationListener {
123
130
}
124
131
125
132
private static Class <?>[] EVENT_TYPES = { ApplicationStartedEvent .class ,
126
- ApplicationEnvironmentPreparedEvent .class , ContextClosedEvent .class };
133
+ ApplicationEnvironmentPreparedEvent .class , ApplicationPreparedEvent .class ,
134
+ ContextClosedEvent .class };
127
135
128
136
private static Class <?>[] SOURCE_TYPES = { SpringApplication .class ,
129
137
ApplicationContext .class };
@@ -168,6 +176,9 @@ else if (event instanceof ApplicationEnvironmentPreparedEvent) {
168
176
onApplicationEnvironmentPreparedEvent (
169
177
(ApplicationEnvironmentPreparedEvent ) event );
170
178
}
179
+ else if (event instanceof ApplicationPreparedEvent ) {
180
+ onApplicationPreparedEvent ((ApplicationPreparedEvent ) event );
181
+ }
171
182
else if (event instanceof ContextClosedEvent ) {
172
183
onContextClosedEvent ();
173
184
}
@@ -188,6 +199,13 @@ private void onApplicationEnvironmentPreparedEvent(
188
199
initialize (event .getEnvironment (), event .getSpringApplication ().getClassLoader ());
189
200
}
190
201
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
+
191
209
private void onContextClosedEvent () {
192
210
if (this .loggingSystem != null ) {
193
211
this .loggingSystem .cleanUp ();
0 commit comments