1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.classic.turbo;
15
16 import org.slf4j.MDC;
17 import org.slf4j.Marker;
18
19 import ch.qos.logback.classic.Level;
20 import ch.qos.logback.classic.Logger;
21 import ch.qos.logback.core.spi.FilterReply;
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 public class MDCFilter extends MatchingFilter {
44
45 String MDCKey;
46 String value;
47
48 @Override
49 public void start() {
50 int errorCount = 0;
51 if (value == null) {
52 addError("\'value\' parameter is mandatory. Cannot start.");
53 errorCount++;
54 }
55 if (MDCKey == null) {
56 addError("\'MDCKey\' parameter is mandatory. Cannot start.");
57 errorCount++;
58 }
59
60 if (errorCount == 0)
61 this.start = true;
62 }
63
64 @Override
65 public FilterReply decide(Marker marker, Logger logger, Level level, String format, Object[] params, Throwable t) {
66
67 if (!isStarted()) {
68 return FilterReply.NEUTRAL;
69 }
70
71 String value = MDC.get(MDCKey);
72 if (this.value.equals(value)) {
73 return onMatch;
74 }
75 return onMismatch;
76 }
77
78 public void setValue(String value) {
79 this.value = value;
80 }
81
82 public void setMDCKey(String MDCKey) {
83 this.MDCKey = MDCKey;
84 }
85
86 }