Skip to content

Commit 9443713

Browse files
committed
Disable Log4j2's shutdown hook with 2.18 and later
Closes gh-31719
1 parent 32b8e6c commit 9443713

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,14 @@ public int getPriority() {
5151
return -200;
5252
}
5353

54+
@Override
55+
public String getProperty(String key) {
56+
return this.properties.get(key);
57+
}
58+
59+
@Override
60+
public boolean containsProperty(String key) {
61+
return this.properties.containsKey(key);
62+
}
63+
5464
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2022 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+
* https://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.util.stream.Stream;
20+
21+
import org.apache.logging.log4j.LogManager;
22+
import org.apache.logging.log4j.core.util.Constants;
23+
import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry;
24+
import org.junit.jupiter.api.Test;
25+
26+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link SpringBootPropertySource}.
32+
*
33+
* @author Andy Wilkinson
34+
*/
35+
@ClassPathExclusions({ "jakarta.servlet-api-*.jar", "tomcat-embed-core-*.jar" })
36+
class SpringBootPropertySourceTests {
37+
38+
@Test
39+
void propertySourceHasDisabledShutdownHook() {
40+
// Log4j2 disables the hook automatically in a web app so we check that it doesn't
41+
// think it's in one
42+
assertThat(Constants.IS_WEB_APP).isFalse();
43+
assertThat(((ShutdownCallbackRegistry) LogManager.getFactory()).addShutdownCallback(() -> {
44+
})).isNull();
45+
}
46+
47+
@Test
48+
void allDefaultMethodsAreImplemented() {
49+
assertThat(Stream.of(SpringBootPropertySource.class.getMethods()).filter((method) -> method.isDefault()))
50+
.isEmpty();
51+
}
52+
53+
}

0 commit comments

Comments
 (0)