From 7d8cb5fe49bcbdc99b02d597ef7ad7e8a9d91d2e Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Mon, 4 Nov 2024 21:22:32 +0200 Subject: [PATCH] Remove explicit '.xml' suffix check from `LogbackLoggingSystem` Update `LogbackLoggingSystem` so that paths suffixes are no longer checked for `.xml`. Since Logback now only supports XML files, we're safe to pass all content along for processing. If the incorrect content is found, Logback will throw an exception. See gh-42986 --- .../boot/logging/logback/LogbackLoggingSystem.java | 13 ++++--------- .../logging/logback/LogbackLoggingSystemTests.java | 14 ++++++++++++-- .../src/test/resources/logback-without-extension | 11 +++++++++++ 3 files changed, 27 insertions(+), 11 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/logback-without-extension 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 009da77db7..adb91a0b34 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2024 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. @@ -281,14 +281,9 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext, URL url) throws JoranException { - if (url.getPath().endsWith(".xml")) { - JoranConfigurator configurator = new SpringBootJoranConfigurator(initializationContext); - configurator.setContext(loggerContext); - configurator.doConfigure(url); - } - else { - throw new IllegalArgumentException("Unsupported file extension in '" + url + "'. Only .xml is supported"); - } + JoranConfigurator configurator = new SpringBootJoranConfigurator(initializationContext); + configurator.setContext(loggerContext); + configurator.doConfigure(url); } private void stopAndReset(LoggerContext loggerContext) { 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 df80c94184..17bac5e3b8 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 @@ -37,6 +37,7 @@ import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.spi.LoggerContextListener; import ch.qos.logback.core.ConsoleAppender; import ch.qos.logback.core.encoder.LayoutWrappingEncoder; +import ch.qos.logback.core.joran.spi.JoranException; import ch.qos.logback.core.rolling.RollingFileAppender; import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy; import ch.qos.logback.core.util.DynamicClassLoadingException; @@ -832,8 +833,17 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThatIllegalStateException() .isThrownBy(() -> initialize(this.initializationContext, "classpath:logback-invalid-format.txt", getLogFile(tmpDir() + "/tmp.log", null))) - .satisfies((ex) -> assertThat(ex.getCause()).isInstanceOf(IllegalArgumentException.class) - .hasMessageStartingWith("Unsupported file extension")); + .satisfies((ex) -> assertThat(ex.getCause()).isInstanceOf(JoranException.class) + .hasMessageStartingWith("Problem parsing XML document. See previously reported errors")); + } + + @Test + void whenConfigLocationIsXmlFileWithoutExtensionShouldWork(CapturedOutput output) { + this.loggingSystem.beforeInitialize(); + initialize(this.initializationContext, "classpath:logback-without-extension", + getLogFile(tmpDir() + "/tmp.log", null)); + this.logger.info("No extension and works!"); + assertThat(output.toString()).contains("No extension and works!"); } @Test diff --git a/spring-boot-project/spring-boot/src/test/resources/logback-without-extension b/spring-boot-project/spring-boot/src/test/resources/logback-without-extension new file mode 100644 index 0000000000..68080b7c97 --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/logback-without-extension @@ -0,0 +1,11 @@ + + + + + %msg + + + + + +