From b396d0d291b627261dd4358a0aa212477ddc362b Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 5 Aug 2015 17:20:10 +0100 Subject: [PATCH] Update logging defaults to show class packaging info and log cause first This commit updates the default logging configuration for both Logback and Log4J 2 to include class packaging information when logging exceptions and to log the root cause first. Closes gh-3398 Closes gh-3399 --- .../logback/DefaultLogbackConfiguration.java | 4 ++-- .../boot/logging/log4j2/log4j2-file.xml | 2 +- .../boot/logging/log4j2/log4j2.xml | 2 +- .../boot/logging/logback/defaults.xml | 4 ++-- .../log4j2/Log4J2LoggingSystemTests.java | 22 ++++++++++++++++++- .../logback/LogbackLoggingSystemTests.java | 21 ++++++++++++++++++ 6 files changed, 48 insertions(+), 7 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java index 45f3d537b0..d57a408a2f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/DefaultLogbackConfiguration.java @@ -48,10 +48,10 @@ class DefaultLogbackConfiguration { private static final String CONSOLE_LOG_PATTERN = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} " + "%clr(%5p) %clr(${PID:- }){magenta} %clr(---){faint} " + "%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} " - + "%clr(:){faint} %m%n%wex"; + + "%clr(:){faint} %m%n%rEx"; private static final String FILE_LOG_PATTERN = "%d{yyyy-MM-dd HH:mm:ss.SSS} %5p " - + "${PID:- } --- [%t] %-40.40logger{39} : %m%n%wex"; + + "${PID:- } --- [%t] %-40.40logger{39} : %m%n%rEx"; private static final Charset UTF8 = Charset.forName("UTF-8"); diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml index aae5083f0b..c455428d01 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2-file.xml @@ -2,7 +2,7 @@ ???? - %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %-40.40c{1.} : %m%n%wEx + %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %-40.40c{1.} : %m%n%rEx diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml index d650fceb1a..f27221050c 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/log4j2/log4j2.xml @@ -2,7 +2,7 @@ ???? - %clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%wEx + %clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%rEx diff --git a/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml b/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml index fa624f8d00..f9fe0237f0 100644 --- a/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml +++ b/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml @@ -9,8 +9,8 @@ initialization performed by Boot - - + + org.springframework.boot diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 2d26aba28b..e5b0145586 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -36,8 +36,8 @@ import org.springframework.util.StringUtils; import com.fasterxml.jackson.databind.ObjectMapper; -import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.Matchers.arrayContaining; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.is; @@ -186,6 +186,26 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { "log4j2.jsn", "log4j2.xml"))); } + @Test + public void exceptionsIncludeClassPackaging() throws Exception { + this.loggingSystem.beforeInitialize(); + this.logger.info("Hidden"); + this.loggingSystem.initialize(null, null, null); + this.output.expect(containsString("[junit-")); + this.logger.warn("Expected exception", new RuntimeException("Expected")); + } + + @Test + public void rootCauseIsLoggedFirst() throws Exception { + this.loggingSystem.beforeInitialize(); + this.logger.info("Hidden"); + this.loggingSystem.initialize(null, null, null); + this.output + .expect(containsString("Wrapped by: java.lang.RuntimeException: Expected")); + this.logger.warn("Expected exception", new RuntimeException("Expected", + new RuntimeException("Cause"))); + } + private static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem { private List availableClasses = new ArrayList(); diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index 358aa7676d..8db5822750 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -42,6 +42,7 @@ import org.springframework.util.StringUtils; import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -237,6 +238,26 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { getLineWithText(file, "Hello world").contains("INFO")); } + @Test + public void exceptionsIncludeClassPackaging() throws Exception { + this.loggingSystem.beforeInitialize(); + this.logger.info("Hidden"); + this.loggingSystem.initialize(this.initializationContext, null, null); + this.output.expect(containsString("[junit-")); + this.logger.warn("Expected exception", new RuntimeException("Expected")); + } + + @Test + public void rootCauseIsLoggedFirst() throws Exception { + this.loggingSystem.beforeInitialize(); + this.logger.info("Hidden"); + this.loggingSystem.initialize(this.initializationContext, null, null); + this.output + .expect(containsString("Wrapped by: java.lang.RuntimeException: Expected")); + this.logger.warn("Expected exception", new RuntimeException("Expected", + new RuntimeException("Cause"))); + } + private String getLineWithText(File file, String outputSearch) throws Exception { return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)), outputSearch);