Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,21 @@ public String toString() {
}
}

private static final class CustomException extends Exception {

private final String errorCode;

private CustomException(final String message, final String errorCode) {
super(message);
this.errorCode = errorCode;
}

@Override
public String toString() {
return getClass().getName() + ": Code: " + errorCode + "; " + getMessage();
}
}

static Stream<SeparatorTestCase> separatorTestCases() {
final String level = LEVEL.toString();
return Stream.of(
Expand Down Expand Up @@ -214,6 +229,14 @@ void full_output_should_match_Throwable_printStackTrace(final String pattern) {
assertThat(actualStackTrace).as("pattern=`%s`", effectivePattern).isEqualTo(expectedStackTrace);
}

@Test
void full_output_should_use_custom_toString() {
final Throwable exception = new CustomException("This is an error message", "ERROR_CODE");
final String expectedStackTrace = renderStackTraceUsingJava(exception);
final String actualStackTrace = convert(patternPrefix, exception);
assertThat(actualStackTrace).isEqualTo(expectedStackTrace);
}

// This test does not provide `separator` and `suffix` options, since the reference output will be obtained from
// `Throwable#printStackTrace()`, which doesn't take these into account.
@ParameterizedTest
Expand Down Expand Up @@ -252,10 +275,14 @@ private String limitLines(final String text, final int maxLineCount) {
}

private String renderStackTraceUsingJava() {
return renderStackTraceUsingJava(EXCEPTION);
}

private String renderStackTraceUsingJava(final Throwable throwable) {
final Charset charset = StandardCharsets.UTF_8;
try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(outputStream, false, charset.name())) {
EXCEPTION.printStackTrace(printStream);
throwable.printStackTrace(printStream);
printStream.flush();
return new String(outputStream.toByteArray(), charset);
} catch (final Exception error) {
Expand Down Expand Up @@ -545,9 +572,13 @@ private static List<Exception> createExceptionsOfDifferentDepths() {
}

static String convert(final String pattern) {
return convert(pattern, EXCEPTION);
}

static String convert(final String pattern, final Throwable throwable) {
final List<PatternFormatter> patternFormatters = PATTERN_PARSER.parse(pattern, false, true, true);
final LogEvent logEvent =
Log4jLogEvent.newBuilder().setThrown(EXCEPTION).setLevel(LEVEL).build();
Log4jLogEvent.newBuilder().setThrown(throwable).setLevel(LEVEL).build();
final StringBuilder buffer = new StringBuilder();
for (final PatternFormatter patternFormatter : patternFormatters) {
patternFormatter.format(logEvent, buffer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,7 @@ private void renderCause(
}

static void renderThrowableMessage(final StringBuilder buffer, final Throwable throwable) {
final String message = throwable.getLocalizedMessage();
buffer.append(throwable.getClass().getName());
if (message != null) {
buffer.append(": ");
buffer.append(message);
}
buffer.append(throwable);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

final void renderStackTraceElements(
Expand Down