From c10f78ec5ad8993ed433352153437e47bdb3c5a4 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 3 Jan 2024 14:51:35 +0000 Subject: [PATCH] Improve handling of Throwable from logging system init Closes gh-38885 --- .../logging/LoggingApplicationListener.java | 4 ++-- .../LoggingApplicationListenerTests.java | 12 +++++++++++- .../src/test/resources/logback-janino.xml | 18 ++++++++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 spring-boot-project/spring-boot/src/test/resources/logback-janino.xml diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index 16027ecabd4..2de4b2534d1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.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. @@ -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(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java index d0831142819..c2625e042bc 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/logging/LoggingApplicationListenerTests.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. @@ -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 "); diff --git a/spring-boot-project/spring-boot/src/test/resources/logback-janino.xml b/spring-boot-project/spring-boot/src/test/resources/logback-janino.xml new file mode 100644 index 00000000000..569b18e17cc --- /dev/null +++ b/spring-boot-project/spring-boot/src/test/resources/logback-janino.xml @@ -0,0 +1,18 @@ + + + + + %property{LOG_FILE} [%t] ${PID:-????} %c{1}: %m%n BOOTBOOT + + + + return level <= DEBUG; + + DENY + NEUTRAL + + + + + +