Skip to content

Commit

Permalink
strip trailing whitespaces after trimming (short message)
Browse files Browse the repository at this point in the history
  • Loading branch information
fupgang committed Mar 4, 2024
1 parent 56be5c2 commit c84ed50
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/main/java/de/siegmar/logbackgelf/GelfEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,13 +438,12 @@ protected String normalizeShortMessage(final String shortMessage) {
return sanitizedShortMessage;
}

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

protected String buildShortMessage(final ILoggingEvent event) {
Expand Down
11 changes: 11 additions & 0 deletions src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ void stripLeadingAndTrailingWhitespaces() {
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 shortenAfterStrippingWhitespaces() {
final String shortMessage = "\t \n" + "A".repeat(250) + "\t \n";
Expand Down

0 comments on commit c84ed50

Please sign in to comment.