From ae6311ddda58ff557894ee2987edc5b487efbea6 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Thu, 16 Jun 2022 15:31:36 -0700 Subject: [PATCH] Prevent Logback from accidentally being used in Log4J2LoggingSystemTests Update `Log4J2LoggingSystemTests` to exclude Logback and include 'log4j-slf4j-impl'. The `ModifiedClassPathClassLoader` has also been updated so that it no longer automatically excludes `log4j` artifacts, instead we now use `@ClassPathExclusions` on the relevant tests. Fixes gh-19365 --- ...csWithLog4jLoggerContextAutoConfigurationTests.java | 4 +++- ...ricsAutoConfigurationWithLog4j2AndLogbackTests.java | 4 +++- .../classpath/ModifiedClassPathClassLoader.java | 9 +++------ .../LogbackAndLog4J2ExcludedLoggingSystemTests.java | 5 ++--- .../boot/logging/log4j2/Log4J2LoggingSystemTests.java | 10 +++++++--- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests.java index 85695565e9c..a38764b8117 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 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. @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -34,6 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@ClassPathExclusions("log4j-to-slf4j-*.jar") @ClassPathOverrides("org.apache.logging.log4j:log4j-core:2.11.1") class Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests { diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationWithLog4j2AndLogbackTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationWithLog4j2AndLogbackTests.java index 7cb08ce22fe..4466d8278c4 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationWithLog4j2AndLogbackTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationWithLog4j2AndLogbackTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2022 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. @@ -21,6 +21,7 @@ import org.junit.jupiter.api.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import static org.assertj.core.api.Assertions.assertThat; @@ -31,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@ClassPathExclusions("log4j-to-slf4j-*.jar") @ClassPathOverrides({ "org.apache.logging.log4j:log4j-core:2.9.0", "org.apache.logging.log4j:log4j-slf4j-impl:2.9.0" }) class MetricsAutoConfigurationWithLog4j2AndLogbackTests { diff --git a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java index adae5409fb7..528e32c5920 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2021 the original author or authors. + * Copyright 2012-2022 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. @@ -242,11 +242,8 @@ final class ModifiedClassPathClassLoader extends URLClassLoader { private final AntPathMatcher matcher = new AntPathMatcher(); private ClassPathEntryFilter(MergedAnnotation annotation) { - this.exclusions = new ArrayList<>(); - this.exclusions.add("log4j-*.jar"); - if (annotation.isPresent()) { - this.exclusions.addAll(Arrays.asList(annotation.getStringArray(MergedAnnotation.VALUE))); - } + this.exclusions = annotation.getValue(MergedAnnotation.VALUE, String[].class).map(Arrays::asList) + .orElse(Collections.emptyList()); } private boolean isExcluded(URL url) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java index 550194cada4..d8731d71a2f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2020 the original author or authors. + * Copyright 2012-2022 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. @@ -28,8 +28,7 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ -// Log4j2 is implicitly excluded due to LOG4J-2030 -@ClassPathExclusions("logback-*.jar") +@ClassPathExclusions({ "log4j-*.jar", "logback-*.jar" }) class LogbackAndLog4J2ExcludedLoggingSystemTests { @Test diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 1da829f8296..1a040a6bbdf 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -40,7 +40,6 @@ import org.apache.logging.log4j.core.util.ShutdownCallbackRegistry; import org.apache.logging.log4j.util.PropertiesUtil; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -50,6 +49,8 @@ import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystemProperties; +import org.springframework.boot.testsupport.classpath.ClassPathExclusions; +import org.springframework.boot.testsupport.classpath.ClassPathOverrides; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.mock.env.MockEnvironment; @@ -74,6 +75,8 @@ import static org.mockito.Mockito.times; * @author Madhura Bhave */ @ExtendWith(OutputCaptureExtension.class) +@ClassPathExclusions("logback-*.jar") +@ClassPathOverrides("org.apache.logging.log4j:log4j-slf4j-impl:2.17.2") class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { private final TestLog4J2LoggingSystem loggingSystem = new TestLog4J2LoggingSystem(); @@ -247,11 +250,12 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { } @Test - @Disabled("Uses Logback unintentionally") void loggingThatUsesJulIsCaptured(CapturedOutput output) { + String name = getClass().getName(); this.loggingSystem.beforeInitialize(); this.loggingSystem.initialize(this.initializationContext, null, null); - java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(getClass().getName()); + this.loggingSystem.setLogLevel(name, LogLevel.TRACE); + java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(name); julLogger.setLevel(java.util.logging.Level.INFO); julLogger.severe("Hello world"); assertThat(output).contains("Hello world");