1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.sift;
15
16 import ch.qos.logback.classic.spi.ILoggingEvent;
17 import ch.qos.logback.core.sift.AbstractDiscriminator;
18 import ch.qos.logback.core.util.OptionHelper;
19
20 import java.util.Map;
21
22
23
24
25
26
27
28
29
30
31 public class MDCBasedDiscriminator extends AbstractDiscriminator<ILoggingEvent> {
32
33 private String key;
34 private String defaultValue;
35
36
37
38
39
40
41 public String getDiscriminatingValue(ILoggingEvent event) {
42
43 Map<String, String> mdcMap = event.getMDCPropertyMap();
44 if (mdcMap == null) {
45 return defaultValue;
46 }
47 String mdcValue = mdcMap.get(key);
48 if (mdcValue == null) {
49 return defaultValue;
50 } else {
51 return mdcValue;
52 }
53 }
54
55 @Override
56 public void start() {
57 int errors = 0;
58 if (OptionHelper.isNullOrEmptyOrAllSpaces(key)) {
59 errors++;
60 addError("The \"Key\" property must be set");
61 }
62 if (OptionHelper.isNullOrEmptyOrAllSpaces(defaultValue)) {
63 errors++;
64 addError("The \"DefaultValue\" property must be set");
65 }
66 if (errors == 0) {
67 started = true;
68 }
69 }
70
71 public String getKey() {
72 return key;
73 }
74
75 public void setKey(String key) {
76 this.key = key;
77 }
78
79
80
81
82
83 public String getDefaultValue() {
84 return defaultValue;
85 }
86
87
88
89
90
91
92
93
94
95
96
97
98 public void setDefaultValue(String defaultValue) {
99 this.defaultValue = defaultValue;
100 }
101 }