@@ -24,48 +24,56 @@ public class SensitiveFilterLayout extends PatternLayout{
2424 private static Gson gson ;
2525
2626 public SensitiveFilterLayout () throws UnsupportedEncodingException , FileNotFoundException , IOException {
27- GsonBuilder gsonBuilder = new GsonBuilder ();
28- gsonBuilder .registerTypeAdapter (SensitiveDataConfigType .class , new SensitiveTagsDeserializer ());
29- gson = gsonBuilder .create ();
30-
31- InputStream in = getClass ().getResourceAsStream ("/AuthorizedNetSensitiveTagsConfig.json" );
32- BufferedReader reader = new BufferedReader (new InputStreamReader (in ));
33- SensitiveDataConfigType configType = gson .fromJson (reader , SensitiveDataConfigType .class );
34- cardPatterns = new Pattern [configType .sensitiveStringRegexes .length ];
27+ try {
28+ GsonBuilder gsonBuilder = new GsonBuilder ();
29+ gsonBuilder .registerTypeAdapter (SensitiveDataConfigType .class , new SensitiveTagsDeserializer ());
30+ gson = gsonBuilder .create ();
31+
32+ InputStream in = getClass ().getResourceAsStream ("/AuthorizedNetSensitiveTagsConfig.json" );
33+ BufferedReader reader = new BufferedReader (new InputStreamReader (in ));
34+ SensitiveDataConfigType configType = gson .fromJson (reader , SensitiveDataConfigType .class );
35+ cardPatterns = new Pattern [configType .sensitiveStringRegexes .length ];
3536
36- for (int i = 0 ; i < configType .sensitiveStringRegexes .length ; i ++) {
37- cardPatterns [i ] = Pattern .compile (configType .sensitiveStringRegexes [i ]);
38- }
39-
40- int noOfSensitiveTags = configType .sensitiveTags .length ;
41- tagPatterns = new Pattern [noOfSensitiveTags ];
42- tagReplacements = new String [noOfSensitiveTags ];
43-
44- for (int j =0 ; j <noOfSensitiveTags ; j ++) {
45- String tagName = configType .sensitiveTags [j ].tagName ;
46- String pattern = configType .sensitiveTags [j ].pattern ;
47- String replacement = configType .sensitiveTags [j ].replacement ;
48-
49- if (pattern != null && !pattern .isEmpty ())
50- tagPatterns [j ] = Pattern .compile ("<" +tagName +">" +pattern +"</" +tagName +">" );
51- else
52- tagPatterns [j ] = Pattern .compile ("<" +tagName +">" +".+" +"</" +tagName +">" );
53- tagReplacements [j ] = "<" +tagName +">" +replacement +"</" +tagName +">" ;
54- }
55- if (reader !=null )
56- reader .close ();
37+ for (int i = 0 ; i < configType .sensitiveStringRegexes .length ; i ++) {
38+ cardPatterns [i ] = Pattern .compile (configType .sensitiveStringRegexes [i ]);
39+ }
40+
41+ int noOfSensitiveTags = configType .sensitiveTags .length ;
42+ tagPatterns = new Pattern [noOfSensitiveTags ];
43+ tagReplacements = new String [noOfSensitiveTags ];
44+
45+ for (int j =0 ; j <noOfSensitiveTags ; j ++) {
46+ String tagName = configType .sensitiveTags [j ].tagName ;
47+ String pattern = configType .sensitiveTags [j ].pattern ;
48+ String replacement = configType .sensitiveTags [j ].replacement ;
49+
50+ if (pattern != null && !pattern .isEmpty ())
51+ tagPatterns [j ] = Pattern .compile ("<" +tagName +">" +pattern +"</" +tagName +">" );
52+ else
53+ tagPatterns [j ] = Pattern .compile ("<" +tagName +">" +".+" +"</" +tagName +">" );
54+ tagReplacements [j ] = "<" +tagName +">" +replacement +"</" +tagName +">" ;
55+ }
56+ if (reader !=null )
57+ reader .close ();
58+ }
59+ catch (Exception e ){
60+ }
5761 }
5862
5963 @ Override
6064 public String format (LoggingEvent event ) {
61- if (event .getMessage () instanceof String ) {
62- String message = event .getRenderedMessage ();
63- String maskXmlMessage = SensitiveFilterLayout .maskSensitiveXmlString (message );
64- String maskCardNumber = SensitiveFilterLayout .maskCreditCards (maskXmlMessage );
65-
66- Throwable throwable = event .getThrowableInformation () != null ? event .getThrowableInformation ().getThrowable () : null ;
67- LoggingEvent maskedEvent = new LoggingEvent (event .fqnOfCategoryClass , Logger .getLogger (event .getLoggerName ()), event .timeStamp , event .getLevel (), maskCardNumber , throwable );
68- return super .format (maskedEvent );
65+ try {
66+ if (event .getMessage () instanceof String ) {
67+ String message = event .getRenderedMessage ();
68+ String maskXmlMessage = SensitiveFilterLayout .maskSensitiveXmlString (message );
69+ String maskCardNumber = SensitiveFilterLayout .maskCreditCards (maskXmlMessage );
70+
71+ Throwable throwable = event .getThrowableInformation () != null ? event .getThrowableInformation ().getThrowable () : null ;
72+ LoggingEvent maskedEvent = new LoggingEvent (event .fqnOfCategoryClass , Logger .getLogger (event .getLoggerName ()), event .timeStamp , event .getLevel (), maskCardNumber , throwable );
73+ return super .format (maskedEvent );
74+ }
75+ }
76+ catch (Exception e ){
6977 }
7078 return null ;
7179 }
0 commit comments