1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package ch.qos.logback.classic.pattern;
16
17
18 import ch.qos.logback.classic.spi.ILoggingEvent;
19 import ch.qos.logback.core.CoreConstants;
20 import org.slf4j.event.KeyValuePair;
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import static ch.qos.logback.classic.pattern.KeyValuePairConverter.*;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 public class MaskedKeyValuePairConverter extends ClassicConverter {
41 public static final String MASK = "XXX";
42 List<String> optionList;
43 List<String> maskList = new ArrayList<>();
44 KeyValuePairConverter.ValueQuoteSpecification valueQuoteSpec = KeyValuePairConverter.ValueQuoteSpecification.DOUBLE;
45
46 public void start() {
47 this.optionList = getOptionList();
48 KeyValuePairConverter.ValueQuoteSpecification extractedSpec = extractSpec(this.optionList);
49 if (extractedSpec == null) {
50 maskList = optionList;
51 } else {
52 valueQuoteSpec = extractedSpec;
53 maskList = optionList.subList(1, optionList.size());
54 }
55
56 checkMaskListForExtraQuoteSpecs(maskList);
57
58 super.start();
59 }
60
61 private void checkMaskListForExtraQuoteSpecs(List<String> maskList) {
62 if(maskList == null || maskList.isEmpty())
63 return;
64 if(maskList.contains(DOUBLE_OPTION_STR)) {
65 addWarn("quote spec "+DOUBLE_OPTION_STR+ " found in the wrong order");
66 }
67 if(maskList.contains(SINGLE_OPTION_STR)) {
68 addWarn("extra quote spec "+SINGLE_OPTION_STR+ " found in the wrong order");
69 }
70 if(maskList.contains(NONE_OPTION_STR)) {
71 addWarn("extra quote spec "+NONE_OPTION_STR+ " found in the wrong order");
72 }
73 }
74
75
76 KeyValuePairConverter.ValueQuoteSpecification extractSpec(List<String> optionList) {
77
78 if (optionList == null || optionList.isEmpty()) {
79 return null;
80 }
81
82 String firstOption = optionList.get(0);
83
84 if (DOUBLE_OPTION_STR.equalsIgnoreCase(firstOption)) {
85 return KeyValuePairConverter.ValueQuoteSpecification.DOUBLE;
86 } else if (SINGLE_OPTION_STR.equalsIgnoreCase(firstOption)) {
87 return KeyValuePairConverter.ValueQuoteSpecification.SINGLE;
88 } else if (NONE_OPTION_STR.equalsIgnoreCase(firstOption)) {
89 return KeyValuePairConverter.ValueQuoteSpecification.NONE;
90 } else {
91 return null;
92 }
93 }
94
95 @Override
96 public String convert(ILoggingEvent event) {
97
98 List<KeyValuePair> kvpList = event.getKeyValuePairs();
99 if (kvpList == null || kvpList.isEmpty()) {
100 return CoreConstants.EMPTY_STRING;
101 }
102
103 StringBuilder sb = new StringBuilder();
104 for (int i = 0; i < kvpList.size(); i++) {
105 KeyValuePair kvp = kvpList.get(i);
106 if (i != 0)
107 sb.append(' ');
108 sb.append(String.valueOf(kvp.key));
109 sb.append('=');
110 Character quoteChar = valueQuoteSpec.asChar();
111 if (quoteChar != null)
112 sb.append(quoteChar);
113 if (maskList.contains(kvp.key))
114 sb.append(MASK);
115 else
116 sb.append(String.valueOf(kvp.value));
117 if (quoteChar != null)
118 sb.append(quoteChar);
119 }
120
121 return sb.toString();
122 }
123 }