Refine exception handling in logging listener

Refine the exception handling block to cover initialization exceptions
without a log file.

Closes gh-23802
pull/23886/head
Phillip Webb 4 years ago
parent c85cc33ca9
commit 70b7326b73

@ -305,26 +305,26 @@ public class LoggingApplicationListener implements GenericApplicationListener {
} }
private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) { private void initializeSystem(ConfigurableEnvironment environment, LoggingSystem system, LogFile logFile) {
LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
String logConfig = StringUtils.trimWhitespace(environment.getProperty(CONFIG_PROPERTY)); String logConfig = StringUtils.trimWhitespace(environment.getProperty(CONFIG_PROPERTY));
if (ignoreLogConfig(logConfig)) { try {
system.initialize(initializationContext, null, logFile); LoggingInitializationContext initializationContext = new LoggingInitializationContext(environment);
} if (ignoreLogConfig(logConfig)) {
else { system.initialize(initializationContext, null, logFile);
try { }
else {
system.initialize(initializationContext, logConfig, logFile); system.initialize(initializationContext, logConfig, logFile);
} }
catch (Exception ex) { }
Throwable exceptionToReport = ex; catch (Exception ex) {
while (exceptionToReport != null && !(exceptionToReport instanceof FileNotFoundException)) { Throwable exceptionToReport = ex;
exceptionToReport = exceptionToReport.getCause(); while (exceptionToReport != null && !(exceptionToReport instanceof FileNotFoundException)) {
} exceptionToReport = exceptionToReport.getCause();
exceptionToReport = (exceptionToReport != null) ? exceptionToReport : ex;
// NOTE: We can't use the logger here to report the problem
System.err.println("Logging system failed to initialize using configuration from '" + logConfig + "'");
exceptionToReport.printStackTrace(System.err);
throw new IllegalStateException(ex);
} }
exceptionToReport = (exceptionToReport != null) ? exceptionToReport : ex;
// NOTE: We can't use the logger here to report the problem
System.err.println("Logging system failed to initialize using configuration from '" + logConfig + "'");
exceptionToReport.printStackTrace(System.err);
throw new IllegalStateException(ex);
} }
} }

Loading…
Cancel
Save