33
33
public class TestnavLogbackEncoder extends LogstashEncoder {
34
34
35
35
// matches exactly 11 digits (\\d{11}) that are not immediately preceded ((?<!\\d)) or followed ((?!\\d)) by another digit.
36
- private final Pattern pattern = Pattern .compile ("(?<!\\ d)\\ d{11}(?!\\ d)" );
36
+ private static final Pattern IDENT = Pattern .compile ("(?<!\\ d)\\ d{11}(?!\\ d)" );
37
+ private static final Pattern BEARER = Pattern .compile ("Bearer [a-zA-Z0-9\\ -_.]+" );
37
38
38
39
@ Setter
39
40
private int maxStackTraceLength = 480 ;
@@ -113,24 +114,14 @@ private void appendStackTraceCauses(ThrowableProxy exception, StringWriter write
113
114
}
114
115
115
116
private String formatMessage (String message ) {
116
- var matcher = pattern .matcher (message );
117
117
118
- if (!matcher .find ()) {
119
- return message ;
120
- }
121
-
122
- matcher .reset ();
123
- var result = new StringBuilder ();
118
+ message = IDENT .matcher (message ).replaceAll (match ->
124
119
125
- while (matcher .find ()) {
126
- var match = matcher .group ();
127
- if (match .charAt (2 ) == '0' || match .charAt (2 ) == '1' ) {
128
- var replacement = match .substring (0 , 6 ) + "xxxxx" ;
129
- matcher .appendReplacement (result , replacement );
130
- }
131
- }
132
- matcher .appendTail (result );
120
+ match .group ().charAt (2 ) < '4' ?
121
+ match .group ().substring (0 , 6 ) + "xxxxx" :
122
+ match .group ().substring (0 , 11 ) + "x"
123
+ );
133
124
134
- return result . toString ( );
125
+ return BEARER . matcher ( message ). replaceAll ( "Bearer token" );
135
126
}
136
127
}
0 commit comments