Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strip leading and trailing, but do not squash inner whitespaces (#100) #101

Closed
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
32 changes: 5 additions & 27 deletions src/main/java/de/siegmar/logbackgelf/GelfEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,34 +438,12 @@ protected String normalizeShortMessage(final String shortMessage) {
return sanitizedShortMessage;
}

private String sanitizeShortMessage(final String sanitizedShortMessage) {
if (sanitizedShortMessage.isEmpty()) {
return sanitizedShortMessage;
private String sanitizeShortMessage(final String shortMessage) {
String sanitized = shortMessage.stripLeading();
if (getMaxShortMessageLength() != 0 && sanitized.length() > getMaxShortMessageLength()) {
sanitized = sanitized.substring(0, getMaxShortMessageLength());
}

final int len = maxShortMessageLength == 0
? sanitizedShortMessage.length()
: Math.min(sanitizedShortMessage.length(), maxShortMessageLength);
final char[] tmp = new char[len];

int iDst = 0;
boolean whitspaceLast = false;
boolean whitespaceStart = true;
for (int iSrc = 0; iSrc < sanitizedShortMessage.length() && iDst < tmp.length; iSrc++) {
final char c = sanitizedShortMessage.charAt(iSrc);
if (Character.isWhitespace(c)) {
if (!whitespaceStart && !whitspaceLast) {
tmp[iDst++] = ' ';
}
whitspaceLast = true;
} else {
tmp[iDst++] = c;
whitspaceLast = false;
whitespaceStart = false;
}
}

return new String(tmp, 0, iDst).trim();
return sanitized.stripTrailing();
}

protected String buildShortMessage(final ILoggingEvent event) {
Expand Down
52 changes: 52 additions & 0 deletions src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,58 @@ void shortenShortMessageDefault() {
.isEqualTo(expectedShortMessage);
}

@Test
void doNotSquashInnerWhitespaces() {
final String shortMessage = "unknown operand:\nx=1 § 1\n ^";

final GelfEncoder gelfEncoder = new GelfEncoder();

final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
assertThat(actual).isEqualTo(shortMessage);
}

@Test
void stripLeadingAndTrailingWhitespaces() {
final String shortMessage = "\t [---] \n";

final GelfEncoder gelfEncoder = new GelfEncoder();

final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
assertThat(actual).isEqualTo("[---]");
}

@Test
void stripTrailingWhitespacesAfterTrimming() {
final String shortMessage = "a b";

final GelfEncoder gelfEncoder = new GelfEncoder();
gelfEncoder.setMaxShortMessageLength(5);

final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
assertThat(actual).isEqualTo("a");
}

@Test
void maxMessageLengthSetAndMessageIsShorter() {
final String shortMessage = "abc";

final GelfEncoder gelfEncoder = new GelfEncoder();
gelfEncoder.setMaxShortMessageLength(5);

final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
assertThat(actual).isEqualTo("abc");
}

@Test
void shortenAfterStrippingWhitespaces() {
final String shortMessage = "\t \n" + "A".repeat(250) + "\t \n";

final GelfEncoder gelfEncoder = new GelfEncoder();

final String actual = gelfEncoder.normalizeShortMessage(shortMessage);
assertThat(actual).hasSize(250).doesNotContainAnyWhitespaces();
}

@Test
void exception() {
encoder.start();
Expand Down
Loading