1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.pattern;
19
20 import org.apache.log4j.Category;
21 import org.apache.log4j.Level;
22 import org.apache.log4j.Logger;
23 import org.apache.log4j.MDC;
24 import org.apache.log4j.NDC;
25 import org.apache.log4j.Priority;
26 import org.apache.log4j.helpers.Loader;
27 import org.apache.log4j.helpers.LogLog;
28 import org.apache.log4j.spi.LocationInfo;
29 import org.apache.log4j.spi.LoggerRepository;
30 import org.apache.log4j.spi.RendererSupport;
31 import org.apache.log4j.spi.ThrowableInformation;
32
33 import java.io.ObjectInputStream;
34 import java.io.ObjectOutputStream;
35 import java.lang.reflect.Method;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.Hashtable;
39 import java.util.Map;
40 import java.util.Set;
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public class LogEvent implements java.io.Serializable {
56
57 private static long startTime = System.currentTimeMillis();
58
59
60 transient public final String fqnOfCategoryClass;
61
62
63
64
65
66
67
68
69
70
71
72 transient private Category logger;
73
74
75
76
77
78
79
80
81
82 final public String categoryName;
83
84
85
86
87
88
89
90
91
92
93
94
95
96 transient public Priority level;
97
98
99 private String ndc;
100
101
102 private Hashtable mdcCopy;
103
104
105
106
107
108
109 private boolean ndcLookupRequired = true;
110
111
112
113
114
115 private boolean mdcCopyLookupRequired = true;
116
117
118 transient private Object message;
119
120
121
122 private String renderedMessage;
123
124
125 private String threadName;
126
127
128
129
130
131 private ThrowableInformation throwableInfo;
132
133
134
135 public final long timeStamp;
136
137 private LocationInfo locationInfo;
138
139
140 static final long serialVersionUID = -868428216207166145L;
141
142 static final Integer[] PARAM_ARRAY = new Integer[1];
143 static final String TO_LEVEL = "toLevel";
144 static final Class[] TO_LEVEL_PARAMS = new Class[] {int.class};
145 static final Hashtable methodCache = new Hashtable(3);
146
147
148
149
150
151
152
153
154
155
156
157 public LogEvent(String fqnOfCategoryClass, Category logger,
158 Priority level, Object message, Throwable throwable) {
159 this.fqnOfCategoryClass = fqnOfCategoryClass;
160 this.logger = logger;
161 this.categoryName = logger.getName();
162 this.level = level;
163 this.message = message;
164 if(throwable != null) {
165 this.throwableInfo = new ThrowableInformation(throwable);
166 }
167 timeStamp = System.currentTimeMillis();
168 }
169
170
171
172
173
174
175
176
177
178
179
180
181 public LogEvent(String fqnOfCategoryClass, Category logger,
182 long timeStamp, Priority level, Object message,
183 Throwable throwable) {
184 this.fqnOfCategoryClass = fqnOfCategoryClass;
185 this.logger = logger;
186 this.categoryName = logger.getName();
187 this.level = level;
188 this.message = message;
189 if(throwable != null) {
190 this.throwableInfo = new ThrowableInformation(throwable);
191 }
192
193 this.timeStamp = timeStamp;
194 }
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211 public LogEvent(final String fqnOfCategoryClass,
212 final Logger logger,
213 final long timeStamp,
214 final Level level,
215 final Object message,
216 final String threadName,
217 final ThrowableInformation throwable,
218 final String ndc,
219 final LocationInfo info,
220 final java.util.Map properties) {
221 super();
222 this.fqnOfCategoryClass = fqnOfCategoryClass;
223 this.logger = logger;
224 if (logger != null) {
225 categoryName = logger.getName();
226 } else {
227 categoryName = null;
228 }
229 this.level = level;
230 this.message = message;
231 if(throwable != null) {
232 this.throwableInfo = throwable;
233 }
234
235 this.timeStamp = timeStamp;
236 this.threadName = threadName;
237 ndcLookupRequired = false;
238 this.ndc = ndc;
239 this.locationInfo = info;
240 mdcCopyLookupRequired = false;
241 if (properties != null) {
242 mdcCopy = new java.util.Hashtable(properties);
243 }
244 }
245
246
247
248
249
250 public LocationInfo getLocationInformation() {
251 if(locationInfo == null) {
252 locationInfo = new LocationInfo(new Throwable(), fqnOfCategoryClass);
253 }
254 return locationInfo;
255 }
256
257
258
259
260 public Level getLevel() {
261 return (Level) level;
262 }
263
264
265
266
267
268 public String getLoggerName() {
269 return categoryName;
270 }
271
272
273
274
275
276
277
278
279
280
281 public
282 Object getMessage() {
283 if(message != null) {
284 return message;
285 } else {
286 return getRenderedMessage();
287 }
288 }
289
290
291
292
293
294
295 public
296 String getNDC() {
297 if(ndcLookupRequired) {
298 ndcLookupRequired = false;
299 ndc = NDC.get();
300 }
301 return ndc;
302 }
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317 public
318 Object getMDC(String key) {
319 Object r;
320
321
322 if(mdcCopy != null) {
323 r = mdcCopy.get(key);
324 if(r != null) {
325 return r;
326 }
327 }
328 return MDC.get(key);
329 }
330
331
332
333
334
335 public
336 void getMDCCopy() {
337 if(mdcCopyLookupRequired) {
338 mdcCopyLookupRequired = false;
339
340
341 Hashtable t = (Hashtable) MDC.getContext();
342 if(t != null) {
343 mdcCopy = (Hashtable) t.clone();
344 }
345 }
346 }
347
348 public
349 String getRenderedMessage() {
350 if(renderedMessage == null && message != null) {
351 if(message instanceof String)
352 renderedMessage = (String) message;
353 else {
354 LoggerRepository repository = logger.getLoggerRepository();
355
356 if(repository instanceof RendererSupport) {
357 RendererSupport rs = (RendererSupport) repository;
358 renderedMessage= rs.getRendererMap().findAndRender(message);
359 } else {
360 renderedMessage = message.toString();
361 }
362 }
363 }
364 return renderedMessage;
365 }
366
367
368
369
370 public static long getStartTime() {
371 return startTime;
372 }
373
374 public
375 String getThreadName() {
376 if(threadName == null)
377 threadName = (Thread.currentThread()).getName();
378 return threadName;
379 }
380
381
382
383
384
385
386
387
388
389 public
390 ThrowableInformation getThrowableInformation() {
391 return throwableInfo;
392 }
393
394
395
396
397 public
398 String[] getThrowableStrRep() {
399
400 if(throwableInfo == null)
401 return null;
402 else
403 return throwableInfo.getThrowableStrRep();
404 }
405
406
407 private
408 void readLevel(ObjectInputStream ois)
409 throws java.io.IOException, ClassNotFoundException {
410
411 int p = ois.readInt();
412 try {
413 String className = (String) ois.readObject();
414 if(className == null) {
415 level = Level.toLevel(p);
416 } else {
417 Method m = (Method) methodCache.get(className);
418 if(m == null) {
419 Class clazz = Loader.loadClass(className);
420
421
422
423
424
425
426 m = clazz.getDeclaredMethod(TO_LEVEL, TO_LEVEL_PARAMS);
427 methodCache.put(className, m);
428 }
429 PARAM_ARRAY[0] = new Integer(p);
430 level = (Level) m.invoke(null, PARAM_ARRAY);
431 }
432 } catch(Exception e) {
433 LogLog.warn("Level deserialization failed, reverting to default.", e);
434 level = Level.toLevel(p);
435 }
436 }
437
438 private void readObject(ObjectInputStream ois)
439 throws java.io.IOException, ClassNotFoundException {
440 ois.defaultReadObject();
441 readLevel(ois);
442
443
444 if(locationInfo == null)
445 locationInfo = new LocationInfo(null, null);
446 }
447
448 private
449 void writeObject(ObjectOutputStream oos) throws java.io.IOException {
450
451
452 this.getThreadName();
453
454
455 this.getRenderedMessage();
456
457
458
459 this.getNDC();
460
461
462
463 this.getMDCCopy();
464
465
466 this.getThrowableStrRep();
467
468 oos.defaultWriteObject();
469
470
471 writeLevel(oos);
472 }
473
474 private
475 void writeLevel(ObjectOutputStream oos) throws java.io.IOException {
476
477 oos.writeInt(level.toInt());
478
479 Class clazz = level.getClass();
480 if(clazz == Level.class) {
481 oos.writeObject(null);
482 } else {
483
484
485
486 oos.writeObject(clazz.getName());
487 }
488 }
489
490
491
492
493
494
495
496
497
498
499
500 public final void setProperty(final String propName,
501 final String propValue) {
502 if (mdcCopy == null) {
503 getMDCCopy();
504 }
505 if (mdcCopy == null) {
506 mdcCopy = new Hashtable();
507 }
508 mdcCopy.put(propName, propValue);
509 }
510
511
512
513
514
515
516
517
518
519
520
521 public final String getProperty(final String key) {
522 Object value = getMDC(key);
523 String retval = null;
524 if (value != null) {
525 retval = value.toString();
526 }
527 return retval;
528 }
529
530
531
532
533
534
535
536 public final boolean locationInformationExists() {
537 return (locationInfo != null);
538 }
539
540
541
542
543
544
545
546
547 public final long getTimeStamp() {
548 return timeStamp;
549 }
550
551
552
553
554
555
556
557
558
559
560
561
562 public Set getPropertyKeySet() {
563 return getProperties().keySet();
564 }
565
566
567
568
569
570
571
572
573
574
575
576
577 public Map getProperties() {
578 getMDCCopy();
579 Map properties;
580 if (mdcCopy == null) {
581 properties = new HashMap();
582 } else {
583 properties = mdcCopy;
584 }
585 return Collections.unmodifiableMap(properties);
586 }
587
588
589
590
591
592
593
594 public String getFQNOfLoggerClass() {
595 return fqnOfCategoryClass;
596 }
597
598
599
600 }