File tree 2 files changed +35
-26
lines changed
main/java/de/siegmar/logbackgelf
test/java/de/siegmar/logbackgelf
2 files changed +35
-26
lines changed Original file line number Diff line number Diff line change @@ -439,33 +439,12 @@ protected String normalizeShortMessage(final String shortMessage) {
439
439
}
440
440
441
441
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 ;
444
447
}
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 ();
469
448
}
470
449
471
450
protected String buildShortMessage (final ILoggingEvent event ) {
Original file line number Diff line number Diff line change @@ -134,6 +134,36 @@ void shortenShortMessageDefault() {
134
134
.isEqualTo (expectedShortMessage );
135
135
}
136
136
137
+ @ Test
138
+ void doNotSquashInnerWhitespaces () {
139
+ final String shortMessage = "unknown operand:\n x=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
+
137
167
@ Test
138
168
void exception () {
139
169
encoder .start ();
You can’t perform that action at this time.
0 commit comments