/
HttpEventCollectorLog4jAppender.java
215 lines (196 loc) · 9.4 KB
/
HttpEventCollectorLog4jAppender.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.splunk.logging;
/*
* Copyright 2013-2014 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
import java.io.Serializable;
import java.nio.charset.Charset;
import java.util.Dictionary;
import java.util.Hashtable;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
/**
* Splunk Http Appender.
*/
@Plugin(name = "SplunkHttp", category = "Core", elementType = "appender", printObject = true)
@SuppressWarnings("serial")
public final class HttpEventCollectorLog4jAppender extends AbstractAppender
{
private HttpEventCollectorSender sender = null;
private final boolean includeLoggerName;
private final boolean includeThreadName;
private final boolean includeMDC;
private final boolean includeException;
private final boolean includeMarker;
private HttpEventCollectorLog4jAppender(final String name,
final String url,
final String token,
final String channel,
final String type,
final String source,
final String sourcetype,
final String messageFormat,
final String host,
final String index,
final Filter filter,
final Layout<? extends Serializable> layout,
final boolean includeLoggerName,
final boolean includeThreadName,
final boolean includeMDC,
final boolean includeException,
final boolean includeMarker,
final boolean ignoreExceptions,
long batchInterval,
long batchCount,
long batchSize,
long retriesOnError,
String sendMode,
String middleware,
final String disableCertificateValidation,
final String eventBodySerializer)
{
super(name, filter, layout, ignoreExceptions);
Dictionary<String, String> metadata = new Hashtable<String, String>();
metadata.put(HttpEventCollectorSender.MetadataHostTag, host != null ? host : "");
metadata.put(HttpEventCollectorSender.MetadataIndexTag, index != null ? index : "");
metadata.put(HttpEventCollectorSender.MetadataSourceTag, source != null ? source : "");
metadata.put(HttpEventCollectorSender.MetadataSourceTypeTag, sourcetype != null ? sourcetype : "");
metadata.put(HttpEventCollectorSender.MetadataMessageFormatTag, messageFormat != null ? messageFormat : "");
this.sender = new HttpEventCollectorSender(url, token, channel, type, batchInterval, batchCount, batchSize, sendMode, metadata);
// plug a user middleware
if (middleware != null && !middleware.isEmpty()) {
try {
this.sender.addMiddleware((HttpEventCollectorMiddleware.HttpSenderMiddleware)(Class.forName(middleware).newInstance()));
} catch (Exception ignored) {}
}
if (eventBodySerializer != null && !eventBodySerializer.isEmpty()) {
try {
this.sender.setEventBodySerializer((EventBodySerializer) Class.forName(eventBodySerializer).newInstance());
} catch (final Exception ignored) {}
}
// plug resend middleware
if (retriesOnError > 0) {
this.sender.addMiddleware(new HttpEventCollectorResendMiddleware(retriesOnError));
}
if (disableCertificateValidation != null && disableCertificateValidation.equalsIgnoreCase("true")) {
this.sender.disableCertificateValidation();
}
this.includeLoggerName = includeLoggerName;
this.includeThreadName = includeThreadName;
this.includeMDC = includeMDC;
this.includeException = includeException;
this.includeMarker = includeMarker;
}
/**
* Create a Http Appender.
* @return The Http Appender.
*/
@PluginFactory
public static HttpEventCollectorLog4jAppender createAppender(
// @formatter:off
@PluginAttribute("url") final String url,
@PluginAttribute("token") final String token,
@PluginAttribute("channel") final String channel,
@PluginAttribute("type") final String type,
@PluginAttribute("name") final String name,
@PluginAttribute("source") final String source,
@PluginAttribute("sourcetype") final String sourcetype,
@PluginAttribute(HttpEventCollectorSender.MetadataMessageFormatTag) final String messageFormat,
@PluginAttribute("host") final String host,
@PluginAttribute("index") final String index,
@PluginAttribute(value = "ignoreExceptions", defaultBoolean = true) final String ignoreExceptions,
@PluginAttribute("batch_size_bytes") final String batchSize,
@PluginAttribute("batch_size_count") final String batchCount,
@PluginAttribute("batch_interval") final String batchInterval,
@PluginAttribute("retries_on_error") final String retriesOnError,
@PluginAttribute("send_mode") final String sendMode,
@PluginAttribute("middleware") final String middleware,
@PluginAttribute("disableCertificateValidation") final String disableCertificateValidation,
@PluginAttribute("eventBodySerializer") final String eventBodySerializer,
@PluginAttribute(value = "includeLoggerName", defaultBoolean = true) final boolean includeLoggerName,
@PluginAttribute(value = "includeThreadName", defaultBoolean = true) final boolean includeThreadName,
@PluginAttribute(value = "includeMDC", defaultBoolean = true) final boolean includeMDC,
@PluginAttribute(value = "includeException", defaultBoolean = true) final boolean includeException,
@PluginAttribute(value = "includeMarker", defaultBoolean = true) final boolean includeMarker,
@PluginElement("Layout") Layout<? extends Serializable> layout,
@PluginElement("Filter") final Filter filter
)
{
if (name == null)
{
LOGGER.error("No name provided for HttpEventCollectorLog4jAppender");
return null;
}
if (url == null)
{
LOGGER.error("No Splunk URL provided for HttpEventCollectorLog4jAppender");
return null;
}
if (token == null)
{
LOGGER.error("No token provided for HttpEventCollectorLog4jAppender");
return null;
}
if (layout == null)
{
layout = PatternLayout.createLayout("%m", null, null, null, Charset.forName("UTF-8"), true, false, null, null);
}
final boolean ignoreExceptionsBool = Boolean.getBoolean(ignoreExceptions);
return new HttpEventCollectorLog4jAppender(
name, url, token, channel, type,
source, sourcetype, messageFormat, host, index,
filter, layout,
includeLoggerName, includeThreadName, includeMDC, includeException, includeMarker,
ignoreExceptionsBool,
parseInt(batchInterval, HttpEventCollectorSender.DefaultBatchInterval),
parseInt(batchCount, HttpEventCollectorSender.DefaultBatchCount),
parseInt(batchSize, HttpEventCollectorSender.DefaultBatchSize),
parseInt(retriesOnError, 0),
sendMode,
middleware,
disableCertificateValidation,
eventBodySerializer
);
}
/**
* Perform Appender specific appending actions.
* @param event The Log event.
*/
@Override
public void append(final LogEvent event)
{
// if an exception was thrown
this.sender.send(
event.getLevel().toString(),
getLayout().toSerializable(event).toString(),
includeLoggerName ? event.getLoggerName() : null,
includeThreadName ? event.getThreadName() : null,
includeMDC ? event.getContextMap() : null,
(!includeException || event.getThrown() == null) ? null : event.getThrown().getMessage(),
includeMarker ? event.getMarker() : null
);
}
@Override
public void stop() {
this.sender.close();
super.stop();
}
}