Improve handling of Throwable from logging system init

Closes gh-38885
This commit is contained in:
Andy Wilkinson 2024-01-03 14:51:35 +00:00
parent 6755a937a2
commit c10f78ec5a
3 changed files with 31 additions and 3 deletions

View File

@ -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.
@ -335,7 +335,7 @@ public class LoggingApplicationListener implements GenericApplicationListener {
system.initialize(initializationContext, logConfig, logFile);
}
}
catch (Exception ex) {
catch (Throwable ex) {
Throwable exceptionToReport = ex;
while (exceptionToReport != null && !(exceptionToReport instanceof FileNotFoundException)) {
exceptionToReport = exceptionToReport.getCause();

View File

@ -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.
@ -166,6 +166,16 @@ class LoggingApplicationListenerTests {
assertThat(this.output).contains("Hello world").doesNotContain("???").startsWith("null ").endsWith("BOOTBOOT");
}
@Test
@ClassPathExclusions("janino-*.jar")
void tryingToUseJaninoWhenItIsNotOnTheClasspathFailsGracefully(CapturedOutput output) {
addPropertiesToEnvironment(this.context, "logging.config=classpath:logback-janino.xml");
assertThatIllegalStateException()
.isThrownBy(() -> this.listener.initialize(this.context.getEnvironment(), this.context.getClassLoader()));
assertThat(output)
.contains("Logging system failed to initialize using configuration from 'classpath:logback-janino.xml'");
}
@Test
void trailingWhitespaceInLoggingConfigShouldBeTrimmed() {
addPropertiesToEnvironment(this.context, "logging.config=classpath:logback-nondefault.xml ");

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%property{LOG_FILE} [%t] ${PID:-????} %c{1}: %m%n BOOTBOOT</pattern>
</encoder>
<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
<evaluator>
<expression>return level &lt;= DEBUG;</expression>
</evaluator>
<OnMismatch>DENY</OnMismatch>
<OnMatch>NEUTRAL</OnMatch>
</filter>
</appender>
<root level="INFO">
<appender-ref ref="CONSOLE" />
</root>
</configuration>