Commit b396d0d2 authored by Andy Wilkinson's avatar Andy Wilkinson

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
parent f7619706
...@@ -48,10 +48,10 @@ class DefaultLogbackConfiguration { ...@@ -48,10 +48,10 @@ class DefaultLogbackConfiguration {
private static final String CONSOLE_LOG_PATTERN = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} " 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(%5p) %clr(${PID:- }){magenta} %clr(---){faint} "
+ "%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} " + "%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 " 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"); private static final Charset UTF8 = Charset.forName("UTF-8");
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<Configuration status="WARN"> <Configuration status="WARN">
<Properties> <Properties>
<Property name="PID">????</Property> <Property name="PID">????</Property>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %-40.40c{1.} : %m%n%wEx</Property> <Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %-40.40c{1.} : %m%n%rEx</Property>
</Properties> </Properties>
<Appenders> <Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true"> <Console name="Console" target="SYSTEM_OUT" follow="true">
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
<Configuration status="WARN"> <Configuration status="WARN">
<Properties> <Properties>
<Property name="PID">????</Property> <Property name="PID">????</Property>
<Property name="LOG_PATTERN">%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</Property> <Property name="LOG_PATTERN">%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</Property>
</Properties> </Properties>
<Appenders> <Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true"> <Console name="Console" target="SYSTEM_OUT" follow="true">
......
...@@ -9,8 +9,8 @@ initialization performed by Boot ...@@ -9,8 +9,8 @@ initialization performed by Boot
<conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" /> <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter" />
<conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" /> <conversionRule conversionWord="wex" converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter" />
<property name="CONSOLE_LOG_PATTERN" value="%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"/> <property name="CONSOLE_LOG_PATTERN" value="%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%rEx"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%t] %-40.40logger{39} : %m%n%wex"/> <property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } --- [%t] %-40.40logger{39} : %m%n%rEx"/>
<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender"> <appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
<destinationLogger>org.springframework.boot</destinationLogger> <destinationLogger>org.springframework.boot</destinationLogger>
......
...@@ -36,8 +36,8 @@ import org.springframework.util.StringUtils; ...@@ -36,8 +36,8 @@ import org.springframework.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.arrayContaining; import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
...@@ -186,6 +186,26 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -186,6 +186,26 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
"log4j2.jsn", "log4j2.xml"))); "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 static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
private List<String> availableClasses = new ArrayList<String>(); private List<String> availableClasses = new ArrayList<String>();
......
...@@ -42,6 +42,7 @@ import org.springframework.util.StringUtils; ...@@ -42,6 +42,7 @@ import org.springframework.util.StringUtils;
import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.LoggerContext;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
...@@ -237,6 +238,26 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { ...@@ -237,6 +238,26 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
getLineWithText(file, "Hello world").contains("INFO")); 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 { private String getLineWithText(File file, String outputSearch) throws Exception {
return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)), return getLineWithText(FileCopyUtils.copyToString(new FileReader(file)),
outputSearch); outputSearch);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment