Skip to content

Commit eccd29e

Browse files
authored
Handle small stack trace limits and empty separators (#1130)
Signed-off-by: Jaehong Park <105151063+PHJ2000@users.noreply.github.com>
1 parent 1ac0f38 commit eccd29e

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎src/main/java/net/logstash/logback/stacktrace/ShortenedThrowableConverter.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,8 +354,9 @@ public String convert(ILoggingEvent event) {
354354
appendRootCauseLast(builder, null, ThrowableProxyUtil.REGULAR_EXCEPTION_INDENT, throwableProxy, stackHashes, event);
355355
}
356356
if (builder.length() > this.maxLength) {
357-
builder.setLength(this.maxLength - ELLIPSIS.length() - getLineSeparator().length());
357+
builder.setLength(Math.max(0, this.maxLength - ELLIPSIS.length() - getLineSeparator().length()));
358358
builder.append(ELLIPSIS).append(getLineSeparator());
359+
builder.setLength(this.maxLength);
359360
}
360361
return builder.toString();
361362
}
@@ -378,7 +379,8 @@ public String convert(ILoggingEvent event) {
378379
* @param lineSeparator the line separator
379380
*/
380381
public void setLineSeparator(String lineSeparator) {
381-
this.lineSeparator = SeparatorParser.parseSeparator(lineSeparator);
382+
String parsedSeparator = SeparatorParser.parseSeparator(lineSeparator);
383+
this.lineSeparator = parsedSeparator == null ? "" : parsedSeparator;
382384
}
383385

384386
public String getLineSeparator() {

‎src/test/java/net/logstash/logback/stacktrace/ShortenedThrowableConverterTest.java‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,38 @@ public void testMaxLength() {
144144
}
145145
}
146146

147+
@Test
148+
public void testMaxLengthShorterThanTruncationSuffix() {
149+
for (String separator : Arrays.asList("\n", "\r\n", "[newline]", "")) {
150+
String suffix = "..." + separator;
151+
for (int maxLength = 1; maxLength <= suffix.length(); maxLength++) {
152+
ShortenedThrowableConverter converter = new ShortenedThrowableConverter();
153+
converter.setLineSeparator(separator);
154+
converter.setMaxLength(maxLength);
155+
converter.start();
156+
157+
assertThat(convert(converter, new RuntimeException("message")))
158+
.isEqualTo(suffix.substring(0, maxLength));
159+
}
160+
}
161+
}
162+
163+
@Test
164+
public void testEmptyLineSeparator() {
165+
RuntimeException throwable = new RuntimeException("message");
166+
ShortenedThrowableConverter reference = new ShortenedThrowableConverter();
167+
reference.setLineSeparator("|");
168+
reference.start();
169+
String expected = convert(reference, throwable).replace("|", "");
170+
171+
for (String separator : Arrays.asList(null, "")) {
172+
ShortenedThrowableConverter converter = new ShortenedThrowableConverter();
173+
converter.setLineSeparator(separator);
174+
converter.start();
175+
assertThat(convert(converter, throwable)).isEqualTo(expected);
176+
}
177+
}
178+
147179
@Test
148180
public void testMaxLength_invalidLength() {
149181
ShortenedThrowableConverter converter = new ShortenedThrowableConverter();

0 commit comments

Comments
 (0)