From 559106f8a9ced7b6328b2557252d70d819d25253 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 10 Dec 2025 18:28:21 +0100 Subject: [PATCH] Add Log4j2 smoke test This commit adds a smoke test that exercises the SpringProfile extension just like the Logback equivalent. Closes gh-48492 --- .../build.gradle | 34 +++++++++++++++ .../log4j2/SampleLog4j2Application.java | 41 ++++++++++++++++++ .../src/main/resources/application.properties | 1 + .../src/main/resources/log4j2-spring.xml | 17 ++++++++ .../log4j2/SampleLog4j2ApplicationTests.java | 42 +++++++++++++++++++ src/checkstyle/checkstyle-suppressions.xml | 1 + 6 files changed, 136 insertions(+) create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/build.gradle create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/java/smoketest/log4j2/SampleLog4j2Application.java create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/application.properties create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/log4j2-spring.xml create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/test/java/smoketest/log4j2/SampleLog4j2ApplicationTests.java diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/build.gradle b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/build.gradle new file mode 100644 index 00000000000..32debae4b65 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/build.gradle @@ -0,0 +1,34 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +plugins { + id "java" +} + +description = "Spring Boot Log4j2 smoke test" + +dependencies { + modules { + module("org.springframework.boot:spring-boot-starter-logging") { + replacedBy("org.springframework.boot:spring-boot-starter-log4j2", "Use Log4j2 instead of Logback") + } + } + + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter")) + implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-log4j2")) + + testImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test")) +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/java/smoketest/log4j2/SampleLog4j2Application.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/java/smoketest/log4j2/SampleLog4j2Application.java new file mode 100644 index 00000000000..c05007d50c5 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/java/smoketest/log4j2/SampleLog4j2Application.java @@ -0,0 +1,41 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.log4j2; + +import jakarta.annotation.PostConstruct; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SampleLog4j2Application { + + private static final Logger logger = LoggerFactory.getLogger(SampleLog4j2Application.class); + + @PostConstruct + public void logSomething() { + logger.debug("Sample Debug Message"); + logger.trace("Sample Trace Message"); + } + + public static void main(String[] args) { + SpringApplication.run(SampleLog4j2Application.class, args).close(); + } + +} diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/application.properties new file mode 100644 index 00000000000..5841dea27d1 --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/application.properties @@ -0,0 +1 @@ +logging.log4j2.config.override=classpath:org/springframework/boot/logging/log4j2/log4j2.xml \ No newline at end of file diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/log4j2-spring.xml b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/log4j2-spring.xml new file mode 100644 index 00000000000..f4151d57b2b --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/main/resources/log4j2-spring.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/test/java/smoketest/log4j2/SampleLog4j2ApplicationTests.java b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/test/java/smoketest/log4j2/SampleLog4j2ApplicationTests.java new file mode 100644 index 00000000000..4a4757a57ac --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-log4j2/src/test/java/smoketest/log4j2/SampleLog4j2ApplicationTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-present the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package smoketest.log4j2; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; + +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(OutputCaptureExtension.class) +class SampleLog4j2ApplicationTests { + + @Test + void testLoadedCustomLogbackConfig(CapturedOutput output) { + SampleLog4j2Application.main(new String[0]); + assertThat(output).contains("Sample Debug Message").doesNotContain("Sample Trace Message"); + } + + @Test + void testProfile(CapturedOutput output) { + SampleLog4j2Application.main(new String[] { "--spring.profiles.active=staging" }); + assertThat(output).contains("Sample Debug Message").contains("Sample Trace Message"); + } + +} diff --git a/src/checkstyle/checkstyle-suppressions.xml b/src/checkstyle/checkstyle-suppressions.xml index 4cfacfefa5b..da4db69a466 100644 --- a/src/checkstyle/checkstyle-suppressions.xml +++ b/src/checkstyle/checkstyle-suppressions.xml @@ -44,6 +44,7 @@ +