Skip to content

Commit

Permalink
Merge pull request #37039 from quaff
Browse files Browse the repository at this point in the history
* pr/37039:
  Polish "Detect logback config location as xml if path ends with .xml"
  Detect logback config location as xml if path ends with .xml

Closes gh-37039
  • Loading branch information
snicoll committed Aug 22, 2023
2 parents 5b68e5b + eed6e19 commit bbc28c1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {

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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,25 @@ void whenConfigurationErrorIsDetectedUnderlyingCausesAreIncludedAsSuppressedExce
.hasAtLeastOneElementOfType(DynamicClassLoadingException.class));
}

@Test
void whenConfigLocationIsNotXmlThenIllegalArgumentExceptionShouldBeThrown() {
this.loggingSystem.beforeInitialize();
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"));
}

@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);
Expand Down
Empty file.

0 comments on commit bbc28c1

Please sign in to comment.