This repository was archived by the owner on Dec 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathLogentriesAppender.java
More file actions
303 lines (270 loc) · 6.67 KB
/
LogentriesAppender.java
File metadata and controls
303 lines (270 loc) · 6.67 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package com.logentries.logback;
import ch.qos.logback.classic.PatternLayout;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.pattern.SyslogStartConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
import ch.qos.logback.core.net.SyslogConstants;
import com.logentries.net.AsyncLogger;
/**
* Logentries appender for logback.
*
* @author Mark Lacomber
* @author Ben McCann
* @author Chris Mowforth
*/
public class LogentriesAppender extends AppenderBase<ILoggingEvent> {
/**
*
*/
private boolean useExceptionFormatter;
/**
* Asynchronous Background logger
*/
private final AsyncLogger le_async;
/**
* Layout
*/
private Layout<ILoggingEvent> layout;
/**
* Facility String
*/
private String facilityStr;
/**
* Default Suffix Pattern
*/
public static final String DEFAULT_SUFFIX_PATTERN = "[%thread] %logger %msg";
private String suffixPattern;
/**
* Creates a new Logentries appender.
*/
public LogentriesAppender() {
this.le_async = new AsyncLogger();
}
/**
* Creates a new Logentries appender.
* <p>Used for unit testing.</p>
* @param logger the {@link AsyncLogger} to dispatch events
*/
public LogentriesAppender(AsyncLogger logger) {
this.le_async = logger;
}
protected AsyncLogger getAsyncLogger() {
return le_async;
}
/*
* Public methods to send logback parameters to AsyncLogger
*/
/**
* Sets the token.
*
* @param token
*/
public void setToken(String token) {
this.le_async.setToken(token);
}
/**
* Set whether to use com.logentries.logback.ExceptionFormatter when creating default layout
*/
public void setUseExceptionFormatter(boolean value) {
this.useExceptionFormatter = value;
}
/**
* Sets the HTTP PUTflag. <p>Send logs via HTTP PUT instead of default Token
* TCP.</p>
*
* @param httpput true to use HTTP PUT API
*/
public void setHttpPut(boolean HttpPut) {
this.le_async.setHttpPut(HttpPut);
}
/**
* Sets the ACCOUNT KEY value for HTTP PUT.
*
* @param account_key
*/
public void setKey(String account_key) {
this.le_async.setKey(account_key);
}
/**
* Sets the LOCATION value for HTTP PUT.
*
* @param log_location
*/
public void setLocation(String log_location) {
this.le_async.setLocation(log_location);
}
/**
* Sets the SSL boolean flag
*
* @param ssl
*/
public void setSsl(boolean ssl) {
this.le_async.setSsl(ssl);
}
/**
* Sets the debug flag.
*
* <p>Appender in debug mode will print error messages on error console.</p>
*
* @param debug debug flag to set
*/
public void setDebug(boolean debug) {
this.le_async.setDebug(debug);
}
/**
* Sets the flag which determines if DataHub instance is used instead of Logentries service.
*
* @param useDataHub set to true to send log messaged to a DataHub instance.
*/
public void setIsUsingDataHub(boolean useDataHub){
this.le_async.setUseDataHub(useDataHub);
}
/**
* Sets the address where DataHub server resides.
*
* @param dataHubAddr address like "127.0.0.1"
*/
public void setDataHubAddr(String dataHubAddr) {
this.le_async.setDataHubAddr(dataHubAddr);
}
/**
* Sets the port number on which DataHub instance waits for log messages.
*
* @param dataHubPort
*/
public void setDataHubPort(int dataHubPort) {
this.le_async.setDataHubPort(dataHubPort);
}
/**
* Determines whether to send HostName alongside with the log message
*
* @param logHostName
*/
public void setLogHostName(boolean logHostName) {
this.le_async.setLogHostName(logHostName);
}
/**
* Sets the HostName from the configuration
*
* @param hostName
*/
public void setHostName(String hostName) {
this.le_async.setHostName(hostName);
}
/**
* Sets LogID parameter from the configuration
*
* @param logID
*/
public void setLogID(String logID) {
this.le_async.setLogID(logID);
}
/**
* Sets the suffixPattern to be the <pattern> field in the .xml configuration file
*
* @param encoder
*/
public void setEncoder(PatternLayoutEncoder encoder) {
this.suffixPattern = encoder.getPattern();
}
@Override
public void start() {
if (layout == null) {
layout = buildLayout();
}
super.start();
}
String getPrefixPattern() {
return "%syslogStart{" + getFacility() + "}%nopex";
}
/**
* Returns the string value of the <b>Facility</b> option.
*
* See {@link #setFacility} for the set of allowed values.
*/
public String getFacility() {
return facilityStr;
}
/**
* The <b>Facility</b> option must be set one of the strings KERN, USER,
* MAIL, DAEMON, AUTH, SYSLOG, LPR, NEWS, UUCP, CRON, AUTHPRIV, FTP, NTP,
* AUDIT, ALERT, CLOCK, LOCAL0, LOCAL1, LOCAL2, LOCAL3, LOCAL4, LOCAL5,
* LOCAL6, LOCAL7. Case is not important.
*
* <p>See {@link SyslogConstants} and RFC 3164 for more information about
* the <b>Facility</b> option.
*/
public void setFacility(String facilityStr) {
if (facilityStr != null) {
facilityStr = facilityStr.trim();
}
this.facilityStr = facilityStr;
}
/**
* Sets the layout for the Appender
*/
public void setLayout(Layout<ILoggingEvent> layout) {
this.layout = layout;
}
public Layout<ILoggingEvent> getLayout() {
return layout;
}
/**
* Implements AppenderSkeleton Append method, handles time and format
*
* @event event to log
*/
@Override
protected void append(ILoggingEvent event) {
// Render the event according to layout
String formattedEvent = layout.doLayout(event);
// Append stack trace if present
if (useExceptionFormatter) {
IThrowableProxy error = event.getThrowableProxy();
if (error != null) {
formattedEvent += ExceptionFormatter.formatException(error);
}
}
// Prepare to be queued
this.le_async.addLineToQueue(formattedEvent);
}
/**
* Closes all connections to Logentries
*/
@Override
public void stop() {
super.stop();
this.le_async.close();
}
public Layout<ILoggingEvent> buildLayout() {
PatternLayout l = new PatternLayout();
l.setContext(getContext());
l.getInstanceConverterMap().put("syslogStart", SyslogStartConverter.class.getName());
if (suffixPattern == null) {
suffixPattern = DEFAULT_SUFFIX_PATTERN;
}
l.setPattern(getPrefixPattern() + suffixPattern);
l.start();
return l;
}
/**
* See {@link #setSuffixPattern(String).
*
* @return
*/
public String getSuffixPattern() {
return suffixPattern;
}
/**
* The <b>suffixPattern</b> option specifies the format of the
* non-standardized part of the message sent to the syslog server.
*
* @param suffixPattern
*/
public void setSuffixPattern(String suffixPattern) {
this.suffixPattern = suffixPattern;
}
}