Restore "exception first" logging

Effectively revert commit b396d0d2 to restore exception first logging
of exceptions for Logback and Log4j2.

This commit also introduces new extended versions of "whitespace"
padding exception loggers so that source jar information is still
included in the stacktrace.

Fixes gh-4247
pull/4242/merge
Phillip Webb 9 years ago
parent 9be0020f7b
commit eeb407881a

@ -0,0 +1,66 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.logging.log4j2;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.ExtendedThrowablePatternConverter;
import org.apache.logging.log4j.core.pattern.PatternConverter;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
/**
* {@link ThrowablePatternConverter} that adds some additional whitespace around the stack
* trace.
*
* @author Vladimir Tsanev
* @author Phillip Webb
* @since 1.3.0
*/
@Plugin(name = "WhitespaceThrowablePatternConverter", category = PatternConverter.CATEGORY)
@ConverterKeys({ "xwEx", "xwThrowable", "xwException" })
public final class ExtendedWhitespaceThrowablePatternConverter
extends ThrowablePatternConverter {
private final ExtendedThrowablePatternConverter delegate;
private ExtendedWhitespaceThrowablePatternConverter(String[] options) {
super("WhitespaceExtendedThrowable", "throwable", options);
this.delegate = ExtendedThrowablePatternConverter.newInstance(options);
}
@Override
public void format(LogEvent event, StringBuilder buffer) {
if (event.getThrown() != null) {
buffer.append(this.options.getSeparator());
this.delegate.format(event, buffer);
buffer.append(this.options.getSeparator());
}
}
/**
* Creates a new instance of the class. Required by Log4J2.
* @param options pattern options, may be null. If first element is "short", only the
* first line of the throwable will be formatted.
* @return a new {@code WhitespaceThrowablePatternConverter}
*/
public static ExtendedWhitespaceThrowablePatternConverter newInstance(
String[] options) {
return new ExtendedWhitespaceThrowablePatternConverter(options);
}
}

@ -37,9 +37,17 @@ public final class WhitespaceThrowablePatternConverter extends ThrowablePatternC
super("WhitespaceThrowable", "throwable", options);
}
@Override
public void format(LogEvent event, StringBuilder buffer) {
if (event.getThrown() != null) {
buffer.append(this.options.getSeparator());
super.format(event, buffer);
buffer.append(this.options.getSeparator());
}
}
/**
* Creates a new instance of the class. Required by Log4J2.
*
* @param options pattern options, may be null. If first element is "short", only the
* first line of the throwable will be formatted.
* @return a new {@code WhitespaceThrowablePatternConverter}
@ -48,13 +56,4 @@ public final class WhitespaceThrowablePatternConverter extends ThrowablePatternC
return new WhitespaceThrowablePatternConverter(options);
}
@Override
public void format(LogEvent event, StringBuilder buffer) {
if (event.getThrown() != null) {
buffer.append(this.options.getSeparator());
super.format(event, buffer);
buffer.append(this.options.getSeparator());
}
}
}

@ -48,10 +48,10 @@ class DefaultLogbackConfiguration {
private static final String CONSOLE_LOG_PATTERN = "%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} "
+ "%clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} "
+ "%clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} "
+ "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%rEx}";
+ "%clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
private static final String FILE_LOG_PATTERN = "%d{yyyy-MM-dd HH:mm:ss.SSS} "
+ "${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%rEx}";
+ "${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}";
private static final Charset UTF8 = Charset.forName("UTF-8");
@ -90,6 +90,7 @@ class DefaultLogbackConfiguration {
private void base(LogbackConfigurator config) {
config.conversionRule("clr", ColorConverter.class);
config.conversionRule("wex", WhitespaceThrowableProxyConverter.class);
config.conversionRule("wEx", ExtendedWhitespaceThrowableProxyConverter.class);
LevelRemappingAppender debugRemapAppender = new LevelRemappingAppender(
"org.springframework.boot");
config.start(debugRemapAppender);

@ -0,0 +1,39 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.logging.logback;
import ch.qos.logback.classic.pattern.ExtendedThrowableProxyConverter;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.core.CoreConstants;
/**
* {@link ExtendedThrowableProxyConverter} that adds some additional whitespace around the
* stack trace.
*
* @author Phillip Webb
* @since 1.3.0
*/
public class ExtendedWhitespaceThrowableProxyConverter
extends ExtendedThrowableProxyConverter {
@Override
protected String throwableProxyToString(IThrowableProxy tp) {
return CoreConstants.LINE_SEPARATOR + super.throwableProxyToString(tp)
+ CoreConstants.LINE_SEPARATOR;
}
}

@ -2,7 +2,7 @@
<Configuration status="WARN">
<Properties>
<Property name="PID">????</Property>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%rEx</Property>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>

@ -2,7 +2,7 @@
<Configuration status="WARN">
<Properties>
<Property name="PID">????</Property>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%rEx</Property>
<Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
<Property name="LOG_LEVEL_PATTERN">%5p</Property>
<Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
</Properties>

@ -8,9 +8,10 @@ initialization performed by Boot
<included>
<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.ExtendedWhitespaceThrowableProxyConverter" />
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%rEx}"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%rEx}"/>
<property name="CONSOLE_LOG_PATTERN" value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<property name="FILE_LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<appender name="DEBUG_LEVEL_REMAPPER" class="org.springframework.boot.logging.logback.LevelRemappingAppender">
<destinationLogger>org.springframework.boot</destinationLogger>

@ -0,0 +1,59 @@
/*
* Copyright 2012-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.logging.log4j2;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.junit.Test;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ExtendedWhitespaceThrowablePatternConverter}.
*
* @author Vladimir Tsanev
* @author Phillip Webb
*/
public class ExtendedWhitespaceThrowablePatternConverterTests {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private final ThrowablePatternConverter converter = ExtendedWhitespaceThrowablePatternConverter
.newInstance(new String[] {});
@Test
public void noStackTrace() throws Exception {
LogEvent event = Log4jLogEvent.newBuilder().build();
StringBuilder builder = new StringBuilder();
this.converter.format(event, builder);
assertThat(builder.toString(), equalTo(""));
}
@Test
public void withStackTrace() throws Exception {
LogEvent event = Log4jLogEvent.newBuilder().setThrown(new Exception()).build();
StringBuilder builder = new StringBuilder();
this.converter.format(event, builder);
assertThat(builder.toString(), startsWith(LINE_SEPARATOR));
assertThat(builder.toString(), endsWith(LINE_SEPARATOR));
}
}

@ -203,20 +203,6 @@ public class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(fileContents, is(expectedOutput));
}
@Test
public void rootCauseIsLoggedFirst() throws Exception {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
Matcher<String> expectedOutput = containsString(
"Wrapped by: " + "java.lang.RuntimeException: Expected");
this.output.expect(expectedOutput);
this.logger.warn("Expected exception",
new RuntimeException("Expected", new RuntimeException("Cause")));
String fileContents = FileCopyUtils
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
assertThat(fileContents, is(expectedOutput));
}
@Test
public void customExceptionConversionWord() throws Exception {
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex");

@ -18,6 +18,7 @@ package org.springframework.boot.logging.log4j2;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.pattern.ThrowablePatternConverter;
import org.junit.Test;
import static org.hamcrest.Matchers.endsWith;
@ -34,7 +35,7 @@ public class WhitespaceThrowablePatternConverterTests {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private final WhitespaceThrowablePatternConverter converter = WhitespaceThrowablePatternConverter
private final ThrowablePatternConverter converter = WhitespaceThrowablePatternConverter
.newInstance(new String[] {});
@Test

@ -0,0 +1,56 @@
/*
* Copyright 2012-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.logging.logback;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.classic.spi.ThrowableProxy;
import org.junit.Test;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ExtendedWhitespaceThrowableProxyConverter}.
*
* @author Phillip Webb
* @author Chanwit Kaewkasi
*/
public class ExtendedWhitespaceThrowableProxyConverterTests {
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private final ExtendedWhitespaceThrowableProxyConverter converter = new ExtendedWhitespaceThrowableProxyConverter();
private final LoggingEvent event = new LoggingEvent();
@Test
public void noStackTrace() throws Exception {
String s = this.converter.convert(this.event);
assertThat(s, equalTo(""));
}
@Test
public void withStackTrace() throws Exception {
this.event.setThrowableProxy(new ThrowableProxy(new RuntimeException()));
String s = this.converter.convert(this.event);
assertThat(s, startsWith(LINE_SEPARATOR));
assertThat(s, endsWith(LINE_SEPARATOR));
}
}

@ -295,21 +295,6 @@ public class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(fileContents, is(expectedOutput));
}
@Test
public void rootCauseIsLoggedFirst() throws Exception {
this.loggingSystem.beforeInitialize();
this.loggingSystem.initialize(this.initializationContext, null,
getLogFile(null, tmpDir()));
Matcher<String> expectedOutput = containsString(
"Wrapped by: " + "java.lang.RuntimeException: Expected");
this.output.expect(expectedOutput);
this.logger.warn("Expected exception",
new RuntimeException("Expected", new RuntimeException("Cause")));
String fileContents = FileCopyUtils
.copyToString(new FileReader(new File(tmpDir() + "/spring.log")));
assertThat(fileContents, is(expectedOutput));
}
@Test
public void customExceptionConversionWord() throws Exception {
System.setProperty("LOG_EXCEPTION_CONVERSION_WORD", "%ex");

Loading…
Cancel
Save