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 package com.mindtree.techworks.insight.appender;
26
27 import java.util.StringTokenizer;
28 import java.util.regex.Pattern;
29
30 import org.apache.log4j.Appender;
31 import org.apache.log4j.Layout;
32 import org.apache.log4j.helpers.OnlyOnceErrorHandler;
33 import org.apache.log4j.spi.ErrorHandler;
34 import org.apache.log4j.spi.Filter;
35 import org.apache.log4j.spi.LoggerRepository;
36 import org.apache.log4j.spi.LoggingEvent;
37
38 import com.mindtree.techworks.insight.Controller;
39 import com.mindtree.techworks.insight.model.LogEventModel;
40 import com.mindtree.techworks.insight.pagination.IPage;
41 import com.mindtree.techworks.insight.pagination.PageSet;
42 import com.mindtree.techworks.insight.receiver.RuntimeNamespaceContainer;
43 import com.mindtree.techworks.insight.spi.LogEvent;
44 import com.mindtree.techworks.insight.spi.LogLevel;
45
46
47
48
49
50
51
52
53
54
55
56
57
58 public class ParsedEventAppender extends LogEventModel implements Appender {
59
60
61
62
63 private static final long serialVersionUID = 217646441758807591L;
64
65
66
67
68 private static final long LOG_EVENT_SEQUENCE_START = 1L;
69
70
71
72
73 public static final String EXCEPTION_STRING = "EXCEPTION";
74 private static final int EXCEPTION_STRING_LENGTH = EXCEPTION_STRING.length();
75
76
77
78
79 private static final Pattern CLASS_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9.]");
80
81
82
83
84 private static final String NAME = "Parsed Event Appender";
85
86
87
88
89 private static ParsedEventAppender instance;
90
91
92
93
94 private ErrorHandler errorHandler;
95
96
97
98
99 private Filter filter;
100
101
102
103
104 private Layout layout;
105
106
107
108
109 private String name = NAME;
110
111
112
113
114 private long nextLogEventSequenceNumber = LOG_EVENT_SEQUENCE_START;
115
116
117
118
119 private static final String LOG_EVENT_MODEL_NAME = "R";
120
121
122
123
124
125
126 private ParsedEventAppender(Controller controller) {
127 super(controller);
128 this.errorHandler = new OnlyOnceErrorHandler();
129 this.controller.setRootLogEventModel(this);
130 setLogEventModelName(LOG_EVENT_MODEL_NAME);
131 }
132
133
134
135
136
137
138 public static final synchronized ParsedEventAppender getInstance(Controller controller) {
139 if (instance == null) {
140 instance = new ParsedEventAppender(controller);
141 }
142 return instance;
143 }
144
145
146
147
148
149
150
151 public void addFilter(Filter newFilter) {
152 this.filter = newFilter;
153 }
154
155
156
157
158
159
160
161 public Filter getFilter() {
162 return this.filter;
163 }
164
165
166
167
168
169
170
171 public void clearFilters() {
172 this.filter = null;
173 }
174
175
176
177
178
179 public void close() {
180
181 }
182
183
184
185
186
187 public void doAppend(LoggingEvent loggingEvent) {
188 appendEvent(loggingEvent instanceof LogEvent ? (LogEvent)loggingEvent : new LogEvent(loggingEvent));
189 }
190
191
192
193
194
195 public synchronized void appendEvent(LogEvent event) {
196 event.setSequenceNumber(this.nextLogEventSequenceNumber);
197 this.nextLogEventSequenceNumber += 1;
198 event.setExceptionName(getExceptionClassName(event));
199 IPage page = pageSet.getLastPage();
200 page.addLogEvent(event);
201 super.fireLogEventAdded(event);
202 }
203
204
205
206
207
208
209
210 public String getName() {
211 return this.name;
212 }
213
214
215
216
217
218
219
220
221 public void setErrorHandler(ErrorHandler handler) {
222 this.errorHandler = handler;
223 }
224
225
226
227
228
229
230
231 public ErrorHandler getErrorHandler() {
232 return this.errorHandler;
233 }
234
235
236
237
238
239
240
241 public void setLayout(Layout layout) {
242 this.layout = layout;
243 }
244
245
246
247
248
249
250 public Layout getLayout() {
251 return this.layout;
252 }
253
254
255
256
257
258
259
260 public void setName(String name) {
261 this.name = name;
262 }
263
264
265
266
267
268 public boolean requiresLayout() {
269 return false;
270 }
271
272
273
274
275
276
277
278 private final String getExceptionClassName(LogEvent event) {
279 String exceptionName = "";
280 int levelInt = event.getLevel().toInt();
281 if ((levelInt == LogLevel.ERROR_INT) || (levelInt == LogLevel.FATAL_INT) || (levelInt == LogLevel.WARN_INT)) {
282
283 String[] throwableInfo = event.getThrowableStrRep();
284 for (int i = 0; i < throwableInfo.length; i++) {
285 if (throwableInfo[i].toUpperCase().indexOf(EXCEPTION_STRING) > -1) {
286 exceptionName = getExceptionNameFromString(throwableInfo[i]);
287 break;
288 }
289 }
290 if (exceptionName.length() == 0) {
291
292 exceptionName = getExceptionNameFromString(event.getMessage().toString());
293 }
294 }
295 return exceptionName;
296 }
297
298
299
300
301
302
303 private final String getExceptionNameFromString(String line) {
304 String exceptionClassName = "";
305 StringTokenizer tokenizer = new StringTokenizer(line);
306 while(tokenizer.hasMoreTokens()) {
307 String token = tokenizer.nextToken();
308 int exceptionIndex = token.toUpperCase().lastIndexOf(EXCEPTION_STRING);
309 if (exceptionIndex > -1) {
310 int startIndex = exceptionIndex;
311 int prevIndex = startIndex - 1;
312 while(prevIndex >= 0) {
313 char[] prevChar = new char[] {token.charAt(prevIndex)};
314 if (CLASS_NAME_PATTERN.matcher(new String(prevChar)).matches()) {
315 startIndex = prevIndex;
316 prevIndex -= 1;
317 } else {
318 break;
319 }
320 }
321 exceptionClassName = token.substring(startIndex,exceptionIndex + EXCEPTION_STRING_LENGTH);
322 break;
323 }
324 }
325 return exceptionClassName;
326 }
327
328
329
330
331
332
333 public void clear(){
334 super.clear();
335
336 this.initializePageSet();
337 this.nextLogEventSequenceNumber = LOG_EVENT_SEQUENCE_START;
338 RuntimeNamespaceContainer.clearLoadedNamespaces();
339 }
340
341
342
343
344 private void initializePageSet() {
345 if(null == pageSet){
346 pageSet = new PageSet();
347 setPageSet(pageSet);
348 }
349 }
350
351 public void setLoggerRepository(LoggerRepository arg0) {
352
353
354 }
355 }