Skip to content

Commit 66261c7

Browse files
committed
strip leading and trailing, but do not squash inner whitespaces (osiegmar#100)
1 parent 7973aba commit 66261c7

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

src/main/java/de/siegmar/logbackgelf/GelfEncoder.java

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -439,33 +439,12 @@ protected String normalizeShortMessage(final String shortMessage) {
439439
}
440440

441441
private String sanitizeShortMessage(final String sanitizedShortMessage) {
442-
if (sanitizedShortMessage.isEmpty()) {
443-
return sanitizedShortMessage;
442+
String stripped = sanitizedShortMessage.strip();
443+
if(getMaxShortMessageLength() != 0 && stripped.length() > getMaxShortMessageLength()) {
444+
return stripped.substring(0, getMaxShortMessageLength());
445+
} else {
446+
return stripped;
444447
}
445-
446-
final int len = maxShortMessageLength == 0
447-
? sanitizedShortMessage.length()
448-
: Math.min(sanitizedShortMessage.length(), maxShortMessageLength);
449-
final char[] tmp = new char[len];
450-
451-
int iDst = 0;
452-
boolean whitspaceLast = false;
453-
boolean whitespaceStart = true;
454-
for (int iSrc = 0; iSrc < sanitizedShortMessage.length() && iDst < tmp.length; iSrc++) {
455-
final char c = sanitizedShortMessage.charAt(iSrc);
456-
if (Character.isWhitespace(c)) {
457-
if (!whitespaceStart && !whitspaceLast) {
458-
tmp[iDst++] = ' ';
459-
}
460-
whitspaceLast = true;
461-
} else {
462-
tmp[iDst++] = c;
463-
whitspaceLast = false;
464-
whitespaceStart = false;
465-
}
466-
}
467-
468-
return new String(tmp, 0, iDst).trim();
469448
}
470449

471450
protected String buildShortMessage(final ILoggingEvent event) {

src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,36 @@ void shortenShortMessageDefault() {
134134
.isEqualTo(expectedShortMessage);
135135
}
136136

137+
@Test
138+
void doNotSquashInnerWhitespaces() {
139+
final String shortMessage = "unknown operand:\nx=1 § 1\n ^";
140+
141+
final GelfEncoder gelfEncoder = new GelfEncoder();
142+
143+
final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
144+
assertThat(actual).isEqualTo(shortMessage);
145+
}
146+
147+
@Test
148+
void stripLeadingAndTrailingWhitespaces() {
149+
final String shortMessage = "\t [---] \n";
150+
151+
final GelfEncoder gelfEncoder = new GelfEncoder();
152+
153+
final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
154+
assertThat(actual).isEqualTo("[---]");
155+
}
156+
157+
@Test
158+
void shortenAfterStrippingWhitespaces() {
159+
final String shortMessage = "\t \n" + "A".repeat(250) + "\t \n";
160+
161+
final GelfEncoder gelfEncoder = new GelfEncoder();
162+
163+
final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
164+
assertThat(actual).hasSize(250).doesNotContainAnyWhitespaces();
165+
}
166+
137167
@Test
138168
void exception() {
139169
encoder.start();

0 commit comments

Comments
 (0)