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 1/4] 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(); From 56be5c2fee4e23b2e017917560dbfc66b7c9f458 Mon Sep 17 00:00:00 2001 From: fupgang <75629871+fupgang@users.noreply.github.com> Date: Fri, 1 Mar 2024 09:40:02 +0100 Subject: [PATCH 2/4] fix checkstyle errors --- src/main/java/de/siegmar/logbackgelf/GelfEncoder.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java b/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java index ca8eec5..1621497 100644 --- a/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java +++ b/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java @@ -439,8 +439,8 @@ protected String normalizeShortMessage(final String shortMessage) { } private String sanitizeShortMessage(final String sanitizedShortMessage) { - String stripped = sanitizedShortMessage.strip(); - if(getMaxShortMessageLength() != 0 && stripped.length() > getMaxShortMessageLength()) { + final String stripped = sanitizedShortMessage.strip(); + if (getMaxShortMessageLength() != 0 && stripped.length() > getMaxShortMessageLength()) { return stripped.substring(0, getMaxShortMessageLength()); } else { return stripped; From c84ed50f0126e75a326b3b1212866918022aef1e Mon Sep 17 00:00:00 2001 From: fupgang <75629871+fupgang@users.noreply.github.com> Date: Mon, 4 Mar 2024 09:06:25 +0100 Subject: [PATCH 3/4] strip trailing whitespaces after trimming (short message) --- src/main/java/de/siegmar/logbackgelf/GelfEncoder.java | 11 +++++------ .../java/de/siegmar/logbackgelf/GelfEncoderTest.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java b/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java index 1621497..1faf313 100644 --- a/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java +++ b/src/main/java/de/siegmar/logbackgelf/GelfEncoder.java @@ -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) { diff --git a/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java b/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java index 0cf5ae6..7c2d111 100644 --- a/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java +++ b/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java @@ -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"; From 88e6dd8805161340c668282c5cc92142a60ca013 Mon Sep 17 00:00:00 2001 From: fupgang <75629871+fupgang@users.noreply.github.com> Date: Tue, 5 Mar 2024 14:40:16 +0100 Subject: [PATCH 4/4] added test case for further branch coverage --- .../java/de/siegmar/logbackgelf/GelfEncoderTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java b/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java index 7c2d111..24ea912 100644 --- a/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java +++ b/src/test/java/de/siegmar/logbackgelf/GelfEncoderTest.java @@ -165,6 +165,17 @@ void stripTrailingWhitespacesAfterTrimming() { 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";