mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
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
This commit is contained in:
parent
252cf94332
commit
ae6311ddda
@ -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 {
|
||||
|
||||
|
@ -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 {
|
||||
|
||||
|
@ -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<ClassPathExclusions> 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) {
|
||||
|
@ -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
|
||||
|
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user