From 66261c747559b4e49e448b74c20d7d6840b66e6b Mon Sep 17 00:00:00 2001 From: fupgang <75629871+fupgang@users.noreply.github.com> Date: Thu, 29 Feb 2024 14:32:29 +0100 Subject: [PATCH] strip leading and trailing, but do not squash inner whitespaces (#100) --- .../de/siegmar/logbackgelf/GelfEncoder.java | 31 +++---------------- .../siegmar/logbackgelf/GelfEncoderTest.java | 30 ++++++++++++++++++ 2 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java b/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java index 2f5e4ac..ca8eec5 100644 --- a/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java +++ b/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java @@ -439,33 +439,12 @@ protected String normalizeShortMessage(final String shortMessage) { } private String sanitizeShortMessage(final String sanitizedShortMessage) { - if (sanitizedShortMessage.isEmpty()) { - return sanitizedShortMessage; + String stripped = sanitizedShortMessage.strip(); + if(getMaxShortMessageLength() != 0 && stripped.length() > getMaxShortMessageLength()) { + return stripped.substring(0, getMaxShortMessageLength()); + } else { + return stripped; } - - 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(); } protected String buildShortMessage(final ILoggingEvent event) { diff --git a/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java b/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java index c53d530..0cf5ae6 100644 --- a/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java +++ b/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java @@ -134,6 +134,36 @@ 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 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();