Skip to content

Commit 8a0f035

Browse files
committed
Disable Log4J2's shutdown hook by default
Log4J2 enables its shutdown hook by default. When the JVM is exiting, this creates a race between logging that happens during the application context being closed and Log4J2 being shut down such that the logging is lost. This commit updates SpringBootConfigurationFactory so that it produces a custom sub-class of DefaultConfiguration that disables the shutdown hook by default. In addition to solving the problem described above, this also aligns the Log4J2 logging system with the logging.register-shutdown-hook property which defaults to false. Closes gh-11360
1 parent 36605b3 commit 8a0f035

File tree

2 files changed

+66
-8
lines changed

2 files changed

+66
-8
lines changed

spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringBootConfigurationFactory.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,12 +27,18 @@
2727
import org.springframework.boot.logging.LoggingSystem;
2828

2929
/**
30-
* Spring Boot {@link ConfigurationFactory} that prevents logger warnings from being
31-
* printed when the application first starts. This factory is ordered last and is
32-
* triggered by a {@code log4j2.springboot} classpath resource (which is bundled in this
33-
* jar). If the {@link Log4J2LoggingSystem} is active, a {@link DefaultConfiguration} is
34-
* returned with the expectation that the system will later re-initialize Log4J2 with the
35-
* correct configuration file.
30+
* Spring Boot {@link ConfigurationFactory} that customizes Log4J2's default configuration
31+
* to:
32+
*
33+
* <ol>
34+
* <li>Prevent logger warnings from being printed when the application first starts.
35+
* <li>Disable its shutdown hook
36+
* </ol>
37+
*
38+
* This factory is ordered last and is triggered by a {@code log4j2.springboot} classpath
39+
* resource (which is bundled in this jar). If the {@link Log4J2LoggingSystem} is active,
40+
* a custom {@link DefaultConfiguration} is returned with the expectation that the system
41+
* will later re-initialize Log4J2 with the correct configuration file.
3642
*
3743
* @author Phillip Webb
3844
* @since 1.5.0
@@ -53,10 +59,18 @@ public Configuration getConfiguration(LoggerContext loggerContext,
5359
ConfigurationSource source) {
5460
if (source != null && source != ConfigurationSource.NULL_SOURCE) {
5561
if (LoggingSystem.get(loggerContext.getClass().getClassLoader()) != null) {
56-
return new DefaultConfiguration();
62+
return new SpringBootConfiguration();
5763
}
5864
}
5965
return null;
6066
}
6167

68+
private static final class SpringBootConfiguration extends DefaultConfiguration {
69+
70+
private SpringBootConfiguration() {
71+
this.isShutdownHookEnabled = false;
72+
}
73+
74+
}
75+
6276
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2012-2018 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.log4j2;
18+
19+
import java.io.ByteArrayInputStream;
20+
import java.io.IOException;
21+
22+
import org.apache.logging.log4j.core.LoggerContext;
23+
import org.apache.logging.log4j.core.config.ConfigurationSource;
24+
import org.junit.Test;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link SpringBootConfigurationFactory}.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
public class SpringBootConfigurationFactoryTests {
34+
35+
@Test
36+
public void producesConfigurationWithShutdownHookDisabled() throws IOException {
37+
ConfigurationSource source = new ConfigurationSource(
38+
new ByteArrayInputStream(new byte[0]));
39+
assertThat(new SpringBootConfigurationFactory()
40+
.getConfiguration(new LoggerContext(""), source).isShutdownHookEnabled())
41+
.isFalse();
42+
}
43+
44+
}

0 commit comments

Comments
 (0)