From 8ed472d6f9fbf238977d2a58335069d5ffc6f3c7 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 28 Oct 2015 14:01:22 +0000 Subject: [PATCH] Update logging listener to use normal stack trace ordering by default Previously, LoggingApplicationListener used %rEx as the default exception conversion word. This would result in the nested causes being logging in reverse order, i.e. the most deeply nested cause would be logged first. This commit updates the default to be %wEx and adds a test to verify the default behaviour. Closes gh-4247 --- .../appendix-application-properties.adoc | 2 +- .../logging/LoggingApplicationListener.java | 5 ++-- ...itional-spring-configuration-metadata.json | 2 +- .../LoggingApplicationListenerTests.java | 24 ++++++++++++++----- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index ad308ab477..f20abf865e 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -58,7 +58,7 @@ content into your application; rather pick only the properties that you need. # LOGGING logging.config= # location of config file (default classpath:logback.xml for logback) - logging.exception-conversion-word=%rEx # conversion word used when logging exceptions + logging.exception-conversion-word=%wEx # conversion word used when logging exceptions logging.file=myapp.log logging.level.*= # levels for loggers, e.g. "logging.level.org.springframework=DEBUG" (TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF) logging.path=/var/log diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index fe5fd89bd4..cd07b5a645 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -200,7 +200,8 @@ public class LoggingApplicationListener implements GenericApplicationListener { } private void onApplicationPreparedEvent(ApplicationPreparedEvent event) { - ConfigurableListableBeanFactory beanFactory = event.getApplicationContext().getBeanFactory(); + ConfigurableListableBeanFactory beanFactory = event.getApplicationContext() + .getBeanFactory(); if (!beanFactory.containsBean(LOGGING_SYSTEM_BEAN_NAME)) { beanFactory.registerSingleton(LOGGING_SYSTEM_BEAN_NAME, this.loggingSystem); } @@ -236,7 +237,7 @@ public class LoggingApplicationListener implements GenericApplicationListener { private String getExceptionConversionWord(ConfigurableEnvironment environment) { RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment, "logging."); - return resolver.getProperty("exception-conversion-word", "%rEx"); + return resolver.getProperty("exception-conversion-word", "%wEx"); } private void initializeEarlyLoggingLevel(ConfigurableEnvironment environment) { diff --git a/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json index fa82f9f70b..6a56f37310 100644 --- a/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -33,7 +33,7 @@ "name": "logging.exception-conversion-word", "type": "java.lang.String", "description": "Conversion word used when logging exceptions.", - "defaultValue": "%rEx", + "defaultValue": "%wEx", "sourceType": "org.springframework.boot.logging.LoggingApplicationListener" }, { diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java index 186c10ecd8..882d89e5bf 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java @@ -336,15 +336,27 @@ public class LoggingApplicationListenerTests { } @Test - public void overrideExceptionConversionWord() throws Exception { - EnvironmentTestUtils.addEnvironment(this.context, - "logging.exceptionConversionWord:%ex"); + public void defaultExceptionConversionWord() throws Exception { this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); this.outputCapture.expect(containsString("Hello world")); - this.outputCapture.expect(not(containsString("???"))); - this.outputCapture.expect(not(containsString("[junit-"))); - this.logger.info("Hello world", new RuntimeException("Expected")); + this.outputCapture.expect( + not(containsString("Wrapped by: java.lang.RuntimeException: Wrapper"))); + this.logger.info("Hello world", + new RuntimeException("Wrapper", new RuntimeException("Expected"))); + } + + @Test + public void overrideExceptionConversionWord() throws Exception { + EnvironmentTestUtils.addEnvironment(this.context, + "logging.exceptionConversionWord:%rEx"); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + this.outputCapture.expect(containsString("Hello world")); + this.outputCapture.expect( + containsString("Wrapped by: java.lang.RuntimeException: Wrapper")); + this.logger.info("Hello world", + new RuntimeException("Wrapper", new RuntimeException("Expected"))); } @Test