-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
AbstractAppenderFactory.java
159 lines (138 loc) · 4.72 KB
/
AbstractAppenderFactory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package io.dropwizard.logging;
import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.filter.ThresholdFilter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
import ch.qos.logback.core.AsyncAppenderBase;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.spi.FilterAttachable;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Strings;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.TimeZone;
/**
* A base implementation of {@link AppenderFactory}.
* <p/>
* <b>Configuration Parameters:</b>
* <table>
* <tr>
* <td>Name</td>
* <td>Default</td>
* <td>Description</td>
* </tr>
* <tr>
* <td>{@code threshold}</td>
* <td>ALL</td>
* <td>The minimum event level the appender will handle.</td>
* </tr>
* <tr>
* <td>{@code logFormat}</td>
* <td>(none)</td>
* <td>An appender-specific log format.</td>
* </tr>
* <tr>
* <td>{@code queueSize}</td>
* <td>{@link AsyncAppenderBase}</td>
* <td>The maximum capacity of the blocking queue.</td>
* </tr>
* <tr>
* <td>{@code includeCallerData}</td>
* <td>{@link AsyncAppenderBase}</td>
* <td>
* Whether to include caller data, required for line numbers.
* Beware, is considered expensive.
* </td>
* </tr>
* <tr>
* <td>{@code discardingThreshold}</td>
* <td>{@link AsyncAppenderBase}</td>
* <td>
* By default, when the blocking queue has 20% capacity remaining,
* it will drop events of level TRACE, DEBUG and INFO, keeping only
* events of level WARN and ERROR. To keep all events, set discardingThreshold to 0.
* </td>
* </tr>
* </table>
*/
public abstract class AbstractAppenderFactory implements AppenderFactory {
@NotNull
protected Level threshold = Level.ALL;
protected String logFormat;
@Min(1)
@Max(Integer.MAX_VALUE)
private int queueSize = AsyncAppenderBase.DEFAULT_QUEUE_SIZE;
private int discardingThreshold = -1;
private boolean includeCallerData = false;
@JsonProperty
public int getQueueSize() {
return queueSize;
}
@JsonProperty
public void setQueueSize(int queueSize) {
this.queueSize = queueSize;
}
@JsonProperty
public int getDiscardingThreshold() {
return discardingThreshold;
}
@JsonProperty
public void setDiscardingThreshold(int discardingThreshold) {
this.discardingThreshold = discardingThreshold;
}
@JsonProperty
public Level getThreshold() {
return threshold;
}
@JsonProperty
public void setThreshold(Level threshold) {
this.threshold = threshold;
}
@JsonProperty
public String getLogFormat() {
return logFormat;
}
@JsonProperty
public void setLogFormat(String logFormat) {
this.logFormat = logFormat;
}
@JsonProperty
public boolean isIncludeCallerData() {
return includeCallerData;
}
@JsonProperty
public void setIncludeCallerData(boolean includeCallerData) {
this.includeCallerData = includeCallerData;
}
protected Appender<ILoggingEvent> wrapAsync(Appender<ILoggingEvent> appender) {
return wrapAsync(appender, appender.getContext());
}
protected Appender<ILoggingEvent> wrapAsync(Appender<ILoggingEvent> appender, Context context) {
final AsyncAppender asyncAppender = new AsyncAppender();
asyncAppender.setIncludeCallerData(includeCallerData);
asyncAppender.setQueueSize(queueSize);
asyncAppender.setDiscardingThreshold(discardingThreshold);
asyncAppender.setContext(context);
asyncAppender.setName("async-" + appender.getName());
asyncAppender.addAppender(appender);
asyncAppender.start();
return asyncAppender;
}
protected void addThresholdFilter(FilterAttachable<ILoggingEvent> appender, Level threshold) {
final ThresholdFilter filter = new ThresholdFilter();
filter.setLevel(threshold.toString());
filter.start();
appender.addFilter(filter);
}
protected DropwizardLayout buildLayout(LoggerContext context, TimeZone timeZone) {
final DropwizardLayout formatter = new DropwizardLayout(context, timeZone);
if (!Strings.isNullOrEmpty(logFormat)) {
formatter.setPattern(logFormat);
}
formatter.start();
return formatter;
}
}