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
203 lines (176 loc) · 4.22 KB
/
LogentriesAppender.java
File metadata and controls
203 lines (176 loc) · 4.22 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
package com.logentries.log4j;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import com.logentries.net.AsyncLogger;
/**
* Logentries appender for log4j.
*
* @author Mark Lacomber
*
*/
public class LogentriesAppender extends AppenderSkeleton {
/*
* Fields
*/
/** Asynchronous Background logger */
AsyncLogger le_async;
public LogentriesAppender()
{
le_async = new AsyncLogger();
}
/*
* Public methods to send log4j parameters to AsyncLogger
*/
/**
* Sets the token
*
* @param token
*/
public void setToken( String token) {
this.le_async.setToken(token);
}
/**
* Sets the HTTP PUT boolean flag. Send logs via HTTP PUT instead of default Token TCP
*
* @param httpput HttpPut flag to set
*/
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. Appender in debug mode will print error messages on
* error console.
*
* @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);
}
/**
* Implements AppenderSkeleton Append method, handles time and format
*
* @event event to log
*/
@Override
protected void append( LoggingEvent event) {
// Render the event according to layout
String formattedEvent = layout.format( event);
// Append stack trace if present and layout does not handle it
if (layout.ignoresThrowable()) {
String[] stack = event.getThrowableStrRep();
if (stack != null && stack.length > 0)
formattedEvent = appendFormatedEvent(formattedEvent, stack);
}
// Prepare to be queued
this.le_async.addLineToQueue(formattedEvent);
}
private static final String EXCEPTION_SEPARATOR = ", ";
static String appendFormatedEvent(final String formattedEvent, final String[] stack) {
final int len = stack.length;
// calculate buffer size
int buffSize = formattedEvent.length()
+ EXCEPTION_SEPARATOR.length()
+ len - 1
;
for (int i = 0; i < len; i++) {
buffSize += stack[i].length();
}
// concatenate
final StringBuilder sb = new StringBuilder(buffSize);
sb.append(formattedEvent);
sb.append(EXCEPTION_SEPARATOR);
for (int i = 0; i < len; i++) {
sb.append(stack[i]);
if (i < len - 1) {
sb.append('\u2028');
}
}
// assert sb.capacity() == sb.length();
return sb.toString();
}
/**
* Closes all connections to Logentries
*/
@Override
public void close() {
this.le_async.close();
}
@Override
public boolean requiresLayout() {
return true;
}
}