1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender;
18
19 import java.io.Serializable;
20 import java.nio.charset.Charset;
21 import java.util.Objects;
22
23 import org.apache.logging.log4j.core.Appender;
24 import org.apache.logging.log4j.core.ErrorHandler;
25 import org.apache.logging.log4j.core.Filter;
26 import org.apache.logging.log4j.core.Layout;
27 import org.apache.logging.log4j.core.LogEvent;
28 import org.apache.logging.log4j.core.config.Configuration;
29 import org.apache.logging.log4j.core.config.Property;
30 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
31 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
32 import org.apache.logging.log4j.core.config.plugins.PluginElement;
33 import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
34 import org.apache.logging.log4j.core.filter.AbstractFilterable;
35 import org.apache.logging.log4j.core.impl.LocationAware;
36 import org.apache.logging.log4j.core.layout.PatternLayout;
37 import org.apache.logging.log4j.core.util.Integers;
38
39
40
41
42
43 public abstract class AbstractAppender extends AbstractFilterable implements Appender, LocationAware {
44
45
46
47
48
49
50 public abstract static class Builder<B extends Builder<B>> extends AbstractFilterable.Builder<B> {
51
52 @PluginBuilderAttribute
53 private boolean ignoreExceptions = true;
54
55 @PluginElement("Layout")
56 private Layout<? extends Serializable> layout;
57
58 @PluginBuilderAttribute
59 @Required(message = "No appender name provided")
60 private String name;
61
62 @PluginConfiguration
63 private Configuration configuration;
64
65 public Configuration getConfiguration() {
66 return configuration;
67 }
68
69 public Layout<? extends Serializable> getLayout() {
70 return layout;
71 }
72
73 public String getName() {
74 return name;
75 }
76
77 public Layout<? extends Serializable> getOrCreateLayout() {
78 if (layout == null) {
79 return PatternLayout.createDefaultLayout();
80 }
81 return layout;
82 }
83
84 public Layout<? extends Serializable> getOrCreateLayout(final Charset charset) {
85 if (layout == null) {
86 return PatternLayout.newBuilder().withCharset(charset).build();
87 }
88 return layout;
89 }
90
91 public boolean isIgnoreExceptions() {
92 return ignoreExceptions;
93 }
94
95 public B setConfiguration(final Configuration configuration) {
96 this.configuration = configuration;
97 return asBuilder();
98 }
99
100 public B setIgnoreExceptions(final boolean ignoreExceptions) {
101 this.ignoreExceptions = ignoreExceptions;
102 return asBuilder();
103 }
104
105 public B setLayout(final Layout<? extends Serializable> layout) {
106 this.layout = layout;
107 return asBuilder();
108 }
109
110 public B setName(final String name) {
111 this.name = name;
112 return asBuilder();
113 }
114
115
116
117
118 @Deprecated
119 public B withConfiguration(final Configuration configuration) {
120 this.configuration = configuration;
121 return asBuilder();
122 }
123
124
125
126
127 @Deprecated
128 public B withIgnoreExceptions(final boolean ignoreExceptions) {
129 return setIgnoreExceptions(ignoreExceptions);
130 }
131
132
133
134
135 @Deprecated
136 public B withLayout(final Layout<? extends Serializable> layout) {
137 return setLayout(layout);
138 }
139
140
141
142
143 @Deprecated
144 public B withName(final String name) {
145 return setName(name);
146 }
147
148 }
149
150 public static int parseInt(final String s, final int defaultValue) {
151 try {
152 return Integers.parseInt(s, defaultValue);
153 } catch (final NumberFormatException e) {
154 LOGGER.error("Could not parse \"{}\" as an integer, using default value {}: {}", s, defaultValue, e);
155 return defaultValue;
156 }
157 }
158 private final String name;
159 private final boolean ignoreExceptions;
160 private final Layout<? extends Serializable> layout;
161
162 private ErrorHandler handler = new DefaultErrorHandler(this);
163
164 @Override
165 public boolean requiresLocation() {
166 return layout != null && layout instanceof LocationAware && ((LocationAware) layout).requiresLocation();
167 }
168
169
170
171
172
173
174
175
176
177 @Deprecated
178 protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout) {
179 this(name, filter, layout, true, Property.EMPTY_ARRAY);
180 }
181
182
183
184
185
186
187
188
189
190
191
192 @Deprecated
193 protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
194 final boolean ignoreExceptions) {
195 this(name, filter, layout, ignoreExceptions, Property.EMPTY_ARRAY);
196 }
197
198
199
200
201
202
203
204
205
206
207
208 protected AbstractAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
209 final boolean ignoreExceptions, final Property[] properties) {
210 super(filter, properties);
211 this.name = Objects.requireNonNull(name, "name");
212 this.layout = layout;
213 this.ignoreExceptions = ignoreExceptions;
214 }
215
216
217
218
219
220
221 public void error(final String msg) {
222 handler.error(msg);
223 }
224
225
226
227
228
229
230
231
232
233 public void error(final String msg, final LogEvent event, final Throwable t) {
234 handler.error(msg, event, t);
235 }
236
237
238
239
240
241
242
243 public void error(final String msg, final Throwable t) {
244 handler.error(msg, t);
245 }
246
247
248
249
250
251
252 @Override
253 public ErrorHandler getHandler() {
254 return handler;
255 }
256
257
258
259
260
261
262 @Override
263 public Layout<? extends Serializable> getLayout() {
264 return layout;
265 }
266
267
268
269
270
271
272 @Override
273 public String getName() {
274 return name;
275 }
276
277
278
279
280
281
282
283 @Override
284 public boolean ignoreExceptions() {
285 return ignoreExceptions;
286 }
287
288
289
290
291
292
293 @Override
294 public void setHandler(final ErrorHandler handler) {
295 if (handler == null) {
296 LOGGER.error("The handler cannot be set to null");
297 return;
298 }
299 if (isStarted()) {
300 LOGGER.error("The handler cannot be changed once the appender is started");
301 return;
302 }
303 this.handler = handler;
304 }
305
306
307
308
309
310
311
312
313 protected Serializable toSerializable(final LogEvent event) {
314 return layout != null ? layout.toSerializable(event) : null;
315 }
316
317 @Override
318 public String toString() {
319 return name;
320 }
321
322 }