Skip to content
This repository was archived by the owner on Jan 21, 2025. It is now read-only.

Commit 79ad924

Browse files
authored
Merge pull request #25 from totalorder/syslog-appender-suppressed
Log suppressed exceptions in MillisecondPrecisionSyslogAppender
2 parents 73a7ebf + 1763c48 commit 79ad924

File tree

1 file changed

+51
-24
lines changed

1 file changed

+51
-24
lines changed

src/main/java/com/spotify/logging/logback/MillisecondPrecisionSyslogAppender.java

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,40 +82,67 @@ protected void postProcess(final Object eventObject, final OutputStream sw) {
8282
}
8383

8484
final ILoggingEvent event = (ILoggingEvent) eventObject;
85-
IThrowableProxy tp = event.getThrowableProxy();
85+
final IThrowableProxy tp = event.getThrowableProxy();
8686

8787
if (tp == null) {
8888
return;
8989
}
9090

9191
final String stackTracePrefix = stackTraceLayout.doLayout(event);
92-
boolean isRootException = true;
93-
while (tp != null) {
94-
final StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
95-
try {
96-
handleThrowableFirstLine(sw, tp, stackTracePrefix, isRootException);
97-
isRootException = false;
98-
for (final StackTraceElementProxy step : stepArray) {
99-
final StringBuilder sb = new StringBuilder();
100-
sb.append(stackTracePrefix).append(step);
101-
sw.write(sb.toString().getBytes());
102-
sw.flush();
103-
}
104-
} catch (IOException e) {
105-
break;
92+
recursiveWrite(sw, stackTracePrefix, tp, 0, null);
93+
}
94+
95+
private void recursiveWrite(
96+
final OutputStream sw,
97+
final String stackTracePrefix,
98+
final IThrowableProxy tp,
99+
final int indent,
100+
final String firstLinePrefix) {
101+
final StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
102+
try {
103+
handleThrowableFirstLine(sw, tp, stackTracePrefix, indent, firstLinePrefix);
104+
for (final StackTraceElementProxy step : stepArray) {
105+
final StringBuilder sb = new StringBuilder();
106+
sb.append(stackTracePrefix);
107+
addIndent(sb, indent);
108+
sb.append(step);
109+
sw.write(sb.toString().getBytes());
110+
sw.flush();
106111
}
107-
tp = tp.getCause();
112+
} catch (IOException e) {
113+
return;
114+
}
115+
116+
final IThrowableProxy[] suppressed = tp.getSuppressed();
117+
if (suppressed != null) {
118+
for (final IThrowableProxy current : suppressed) {
119+
recursiveWrite(sw, stackTracePrefix, current, indent + 1, CoreConstants.SUPPRESSED);
120+
}
121+
}
122+
123+
final IThrowableProxy cause = tp.getCause();
124+
if (cause != null) {
125+
recursiveWrite(sw, stackTracePrefix, cause, indent, CoreConstants.CAUSED_BY);
108126
}
109127
}
110128

111-
private void handleThrowableFirstLine(final OutputStream sw, final IThrowableProxy tp,
112-
final String stackTracePrefix,
113-
final boolean isRootException)
114-
throws IOException {
115-
final StringBuilder sb = new StringBuilder().append(stackTracePrefix);
129+
private void addIndent(final StringBuilder sb, final int indent) {
130+
for (int i = 0; i < indent; i++) {
131+
sb.append(CoreConstants.TAB);
132+
}
133+
}
116134

117-
if (!isRootException) {
118-
sb.append(CoreConstants.CAUSED_BY);
135+
// LOGBACK-411 and LOGBACK-750
136+
private void handleThrowableFirstLine(
137+
final OutputStream sw,
138+
final IThrowableProxy tp,
139+
final String stackTracePrefix,
140+
final int indent,
141+
final String prefix) throws IOException {
142+
StringBuilder sb = new StringBuilder().append(stackTracePrefix);
143+
addIndent(sb, indent);
144+
if (prefix != null) {
145+
sb.append(prefix);
119146
}
120147
sb.append(tp.getClassName()).append(": ").append(tp.getMessage());
121148
sw.write(sb.toString().getBytes());
@@ -126,7 +153,7 @@ private void handleThrowableFirstLine(final OutputStream sw, final IThrowablePro
126153
public Layout<ILoggingEvent> buildLayout() {
127154
final PatternLayout layout = new PatternLayout();
128155
layout.getInstanceConverterMap().put("syslogStart",
129-
MillisecondPrecisionSyslogStartConverter.class.getName());
156+
MillisecondPrecisionSyslogStartConverter.class.getName());
130157
if (suffixPattern == null) {
131158
suffixPattern = DEFAULT_SUFFIX_PATTERN;
132159
}

0 commit comments

Comments
 (0)