Skip to content

Commit 559106f

Browse files
committed
Add Log4j2 smoke test
This commit adds a smoke test that exercises the SpringProfile extension just like the Logback equivalent. Closes gh-48492
1 parent 3313657 commit 559106f

File tree

6 files changed

+136
-0
lines changed

6 files changed

+136
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2012-present 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+
plugins {
18+
id "java"
19+
}
20+
21+
description = "Spring Boot Log4j2 smoke test"
22+
23+
dependencies {
24+
modules {
25+
module("org.springframework.boot:spring-boot-starter-logging") {
26+
replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback")
27+
}
28+
}
29+
30+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter"))
31+
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-log4j2"))
32+
33+
testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2012-present 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 smoketest.log4j2;
18+
19+
import jakarta.annotation.PostConstruct;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import org.springframework.boot.SpringApplication;
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
26+
@SpringBootApplication
27+
public class SampleLog4j2Application {
28+
29+
private static final Logger logger = LoggerFactory.getLogger(SampleLog4j2Application.class);
30+
31+
@PostConstruct
32+
public void logSomething() {
33+
logger.debug("Sample Debug Message");
34+
logger.trace("Sample Trace Message");
35+
}
36+
37+
public static void main(String[] args) {
38+
SpringApplication.run(SampleLog4j2Application.class, args).close();
39+
}
40+
41+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
logging.log4j2.config.override=classpath:org/springframework/boot/logging/log4j2/log4j2.xml
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration status="WARN">
3+
<SpringProfile name="staging">
4+
<Loggers>
5+
<Logger name="smoketest.log4j2" level="TRACE"/>
6+
<Root level="INFO">
7+
<AppenderRef ref="Console"/>
8+
</Root>
9+
</Loggers>
10+
</SpringProfile>
11+
<Loggers>
12+
<Logger name="smoketest.log4j2" level="DEBUG"/>
13+
<Root level="INFO">
14+
<AppenderRef ref="Console"/>
15+
</Root>
16+
</Loggers>
17+
</Configuration>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-present 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 smoketest.log4j2;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
22+
import org.springframework.boot.test.system.CapturedOutput;
23+
import org.springframework.boot.test.system.OutputCaptureExtension;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@ExtendWith(OutputCaptureExtension.class)
28+
class SampleLog4j2ApplicationTests {
29+
30+
@Test
31+
void testLoadedCustomLogbackConfig(CapturedOutput output) {
32+
SampleLog4j2Application.main(new String[0]);
33+
assertThat(output).contains("Sample Debug Message").doesNotContain("Sample Trace Message");
34+
}
35+
36+
@Test
37+
void testProfile(CapturedOutput output) {
38+
SampleLog4j2Application.main(new String[] { "--spring.profiles.active=staging" });
39+
assertThat(output).contains("Sample Debug Message").contains("Sample Trace Message");
40+
}
41+
42+
}

src/checkstyle/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<suppress files="SpringBootJoranConfiguratorTests\.java" checks="IllegalImport" />
4545
<suppress files="LogbackMetricsAutoConfiguration\.java" checks="IllegalImport" />
4646
<suppress files="RemoteUrlPropertyExtractorTests\.java" checks="IllegalImport" />
47+
<suppress files="SampleLog4j2Application\.java" checks="IllegalImport" />
4748
<suppress files="SampleLogbackApplication\.java" checks="IllegalImport" />
4849
<suppress files="[\\/]src[\\/]test[\\/]java[\\/]org[\\/]springframework[\\/]boot[\\/]test[\\/]rule[\\/]" checks="SpringJUnit5" />
4950
<suppress files="MockBeanWithSpringMethodRuleRepeatJUnit4IntegrationTests" checks="SpringJUnit5" />

0 commit comments

Comments
 (0)