1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.sift;
15
16 import ch.qos.logback.core.Appender;
17 import ch.qos.logback.core.AppenderBase;
18 import ch.qos.logback.core.model.SiftModel;
19 import ch.qos.logback.core.util.Duration;
20
21
22
23
24
25
26
27
28
29
30
31 public abstract class SiftingAppenderBase<E> extends AppenderBase<E> {
32
33 protected AppenderTracker<E> appenderTracker;
34 AppenderFactory<E> appenderFactory;
35 Duration timeout = new Duration(AppenderTracker.DEFAULT_TIMEOUT);
36 int maxAppenderCount = AppenderTracker.DEFAULT_MAX_COMPONENTS;
37
38 SiftModel siftModel;
39 Discriminator<E> discriminator;
40
41 public Duration getTimeout() {
42 return timeout;
43 }
44
45 public void setTimeout(Duration timeout) {
46 this.timeout = timeout;
47 }
48
49 public SiftModel getSiftModel() {
50 return siftModel;
51 }
52
53 public void setSiftModel(SiftModel siftModel) {
54 this.siftModel = siftModel;
55 }
56
57 public int getMaxAppenderCount() {
58 return maxAppenderCount;
59 }
60
61 public void setMaxAppenderCount(int maxAppenderCount) {
62 this.maxAppenderCount = maxAppenderCount;
63 }
64
65
66
67
68
69 public void setAppenderFactory(AppenderFactory<E> appenderFactory) {
70 this.appenderFactory = appenderFactory;
71 }
72
73 @Override
74 public void start() {
75 int errors = 0;
76 if (discriminator == null) {
77 addError("Missing discriminator. Aborting");
78 errors++;
79 }
80 if (!discriminator.isStarted()) {
81 addError("Discriminator has not started successfully. Aborting");
82 errors++;
83 }
84 if (appenderFactory == null) {
85 addError("AppenderFactory has not been set. Aborting");
86 errors++;
87 } else {
88 appenderTracker = new AppenderTracker<E>(context, appenderFactory);
89 appenderTracker.setMaxComponents(maxAppenderCount);
90 appenderTracker.setTimeout(timeout.getMilliseconds());
91 }
92 if (errors == 0) {
93 super.start();
94 }
95 }
96
97 @Override
98 public void stop() {
99 if(!isStarted())
100 return;
101 for (Appender<E> appender : appenderTracker.allComponents()) {
102 appender.stop();
103 }
104 }
105
106 abstract protected long getTimestamp(E event);
107
108 @Override
109 protected void append(E event) {
110 if (!isStarted()) {
111 return;
112 }
113 String discriminatingValue = discriminator.getDiscriminatingValue(event);
114 long timestamp = getTimestamp(event);
115
116 Appender<E> appender = appenderTracker.getOrCreate(discriminatingValue, timestamp);
117
118 if (eventMarksEndOfLife(event)) {
119 appenderTracker.endOfLife(discriminatingValue);
120 }
121 appenderTracker.removeStaleComponents(timestamp);
122 appender.doAppend(event);
123 }
124
125 protected abstract boolean eventMarksEndOfLife(E event);
126
127 public Discriminator<E> getDiscriminator() {
128 return discriminator;
129 }
130
131 public void setDiscriminator(Discriminator<E> discriminator) {
132 this.discriminator = discriminator;
133 }
134
135
136
137
138
139
140
141
142
143
144 public AppenderTracker<E> getAppenderTracker() {
145 return appenderTracker;
146 }
147
148 public String getDiscriminatorKey() {
149 if (discriminator != null) {
150 return discriminator.getKey();
151 } else {
152 return null;
153 }
154 }
155 }