From c9cc1da48e519300348e6a4b113d5f3a42a4d4d6 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 21 Aug 2023 11:07:15 +0800 Subject: [PATCH] Detect logback config location as xml if path ends with .xml See gh-37039 --- .../logging/logback/LogbackLoggingSystem.java | 2 +- .../logback/LogbackLoggingSystemTests.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index fd3de3a644..ad46546beb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -273,7 +273,7 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext, URL url) throws JoranException { - if (url.toString().endsWith(".xml")) { + if (url.getPath().endsWith(".xml")) { JoranConfigurator configurator = new SpringBootJoranConfigurator(initializationContext); configurator.setContext(loggerContext); configurator.doConfigure(url); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index f46b40e84b..1fc870d66f 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -687,6 +687,25 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { .hasAtLeastOneElementOfType(DynamicClassLoadingException.class)); } + @Test + void whenConfigLocationIsNotXmlThenIllegalArgumentExceptionShouldBeThrown() { + this.loggingSystem.beforeInitialize(); + assertThatIllegalStateException() + .isThrownBy(() -> initialize(this.initializationContext, "file:///logback-nonexistent.txt", + getLogFile(tmpDir() + "/tmp.log", null))) + .satisfies((ex) -> assertThat(ex.getCause()).isInstanceOf(IllegalArgumentException.class) + .hasMessageStartingWith("Unsupported file extension")); + } + + @Test + void whenConfigLocationIsXmlAndHasQueryParametersThenIllegalArgumentExceptionShouldNotBeThrown() { + this.loggingSystem.beforeInitialize(); + assertThatIllegalStateException() + .isThrownBy(() -> initialize(this.initializationContext, "file:///logback-nonexistent.xml?raw=true", + getLogFile(tmpDir() + "/tmp.log", null))) + .satisfies((ex) -> assertThat(ex.getCause()).isNotInstanceOf(IllegalArgumentException.class)); + } + private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) { this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile); this.loggingSystem.initialize(context, configLocation, logFile);