Skip to content

Commit ae6311d

Browse files
committed
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
1 parent 252cf94 commit ae6311d

File tree

5 files changed

+18
-14
lines changed

5 files changed

+18
-14
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -23,6 +23,7 @@
2323
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
2424
import org.springframework.boot.autoconfigure.AutoConfigurations;
2525
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
2627
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
2728
import org.springframework.context.annotation.Bean;
2829
import org.springframework.context.annotation.Configuration;
@@ -34,6 +35,7 @@
3435
*
3536
* @author Andy Wilkinson
3637
*/
38+
@ClassPathExclusions("log4j-to-slf4j-*.jar")
3739
@ClassPathOverrides("org.apache.logging.log4j:log4j-core:2.11.1")
3840
class Log4J2MetricsWithLog4jLoggerContextAutoConfigurationTests {
3941

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/metrics/MetricsAutoConfigurationWithLog4j2AndLogbackTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.boot.autoconfigure.AutoConfigurations;
2323
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
24+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
2425
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
2526

2627
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,6 +32,7 @@
3132
*
3233
* @author Andy Wilkinson
3334
*/
35+
@ClassPathExclusions("log4j-to-slf4j-*.jar")
3436
@ClassPathOverrides({ "org.apache.logging.log4j:log4j-core:2.9.0", "org.apache.logging.log4j:log4j-slf4j-impl:2.9.0" })
3537
class MetricsAutoConfigurationWithLog4j2AndLogbackTests {
3638

spring-boot-project/spring-boot-tools/spring-boot-test-support/src/main/java/org/springframework/boot/testsupport/classpath/ModifiedClassPathClassLoader.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -242,11 +242,8 @@ private static final class ClassPathEntryFilter {
242242
private final AntPathMatcher matcher = new AntPathMatcher();
243243

244244
private ClassPathEntryFilter(MergedAnnotation<ClassPathExclusions> annotation) {
245-
this.exclusions = new ArrayList<>();
246-
this.exclusions.add("log4j-*.jar");
247-
if (annotation.isPresent()) {
248-
this.exclusions.addAll(Arrays.asList(annotation.getStringArray(MergedAnnotation.VALUE)));
249-
}
245+
this.exclusions = annotation.getValue(MergedAnnotation.VALUE, String[].class).map(Arrays::asList)
246+
.orElse(Collections.emptyList());
250247
}
251248

252249
private boolean isExcluded(URL url) {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/LogbackAndLog4J2ExcludedLoggingSystemTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 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.
@@ -28,8 +28,7 @@
2828
*
2929
* @author Andy Wilkinson
3030
*/
31-
// Log4j2 is implicitly excluded due to LOG4J-2030
32-
@ClassPathExclusions("logback-*.jar")
31+
@ClassPathExclusions({ "log4j-*.jar", "logback-*.jar" })
3332
class LogbackAndLog4J2ExcludedLoggingSystemTests {
3433

3534
@Test

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.apache.logging.log4j.util.PropertiesUtil;
4141
import org.junit.jupiter.api.AfterEach;
4242
import org.junit.jupiter.api.BeforeEach;
43-
import org.junit.jupiter.api.Disabled;
4443
import org.junit.jupiter.api.Test;
4544
import org.junit.jupiter.api.extension.ExtendWith;
4645

@@ -50,6 +49,8 @@
5049
import org.springframework.boot.logging.LoggingInitializationContext;
5150
import org.springframework.boot.logging.LoggingSystem;
5251
import org.springframework.boot.logging.LoggingSystemProperties;
52+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
53+
import org.springframework.boot.testsupport.classpath.ClassPathOverrides;
5354
import org.springframework.boot.testsupport.system.CapturedOutput;
5455
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
5556
import org.springframework.mock.env.MockEnvironment;
@@ -74,6 +75,8 @@
7475
* @author Madhura Bhave
7576
*/
7677
@ExtendWith(OutputCaptureExtension.class)
78+
@ClassPathExclusions("logback-*.jar")
79+
@ClassPathOverrides("org.apache.logging.log4j:log4j-slf4j-impl:2.17.2")
7780
class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
7881

7982
private final TestLog4J2LoggingSystem loggingSystem = new TestLog4J2LoggingSystem();
@@ -247,11 +250,12 @@ void setLevelOfUnconfiguredLoggerDoesNotAffectRootConfiguration(CapturedOutput o
247250
}
248251

249252
@Test
250-
@Disabled("Uses Logback unintentionally")
251253
void loggingThatUsesJulIsCaptured(CapturedOutput output) {
254+
String name = getClass().getName();
252255
this.loggingSystem.beforeInitialize();
253256
this.loggingSystem.initialize(this.initializationContext, null, null);
254-
java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(getClass().getName());
257+
this.loggingSystem.setLogLevel(name, LogLevel.TRACE);
258+
java.util.logging.Logger julLogger = java.util.logging.Logger.getLogger(name);
255259
julLogger.setLevel(java.util.logging.Level.INFO);
256260
julLogger.severe("Hello world");
257261
assertThat(output).contains("Hello world");

0 commit comments

Comments
 (0)