Detect logback config location as xml if path ends with .xml

See gh-37039
This commit is contained in:
Yanming Zhou 2023-08-21 11:07:15 +08:00 committed by Stephane Nicoll
parent 5b68e5b9fc
commit c9cc1da48e
2 changed files with 20 additions and 1 deletions

View File

@ -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);

View File

@ -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);