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
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);
|
||||
}
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue