1 16 17 package org.apache.log4j.spi; 18 19 import org.apache.log4j.*; 20 21 import org.apache.log4j.helpers.LogLog; 22 import org.apache.log4j.helpers.Loader; 23 import java.lang.reflect.Method ; 24 import java.io.ObjectOutputStream ; 25 import java.io.ObjectInputStream ; 26 import java.util.Hashtable ; 27 28 32 44 public class LoggingEvent implements java.io.Serializable { 45 46 private static long startTime = System.currentTimeMillis(); 47 48 49 transient public final String fqnOfCategoryClass; 50 51 61 transient private Category logger; 62 63 71 final public String categoryName; 72 73 85 transient public Priority level; 86 87 88 private String ndc; 89 90 91 private Hashtable mdcCopy; 92 93 94 98 private boolean ndcLookupRequired = true; 99 100 101 104 private boolean mdcCopyLookupRequired = true; 105 106 107 transient private Object message; 108 109 111 private String renderedMessage; 112 113 114 private String threadName; 115 116 117 120 private ThrowableInformation throwableInfo; 121 122 124 public final long timeStamp; 125 126 private LocationInfo locationInfo; 127 128 static final long serialVersionUID = -868428216207166145L; 130 131 static final Integer [] PARAM_ARRAY = new Integer [1]; 132 static final String TO_LEVEL = "toLevel"; 133 static final Class [] TO_LEVEL_PARAMS = new Class [] {int.class}; 134 static final Hashtable methodCache = new Hashtable (3); 136 146 public LoggingEvent(String fqnOfCategoryClass, Category logger, 147 Priority level, Object message, Throwable throwable) { 148 this.fqnOfCategoryClass = fqnOfCategoryClass; 149 this.logger = logger; 150 this.categoryName = logger.getName(); 151 this.level = level; 152 this.message = message; 153 if(throwable != null) { 154 this.throwableInfo = new ThrowableInformation(throwable); 155 } 156 timeStamp = System.currentTimeMillis(); 157 } 158 159 170 public LoggingEvent(String fqnOfCategoryClass, Category logger, 171 long timeStamp, Priority level, Object message, 172 Throwable throwable) { 173 this.fqnOfCategoryClass = fqnOfCategoryClass; 174 this.logger = logger; 175 this.categoryName = logger.getName(); 176 this.level = level; 177 this.message = message; 178 if(throwable != null) { 179 this.throwableInfo = new ThrowableInformation(throwable); 180 } 181 182 this.timeStamp = timeStamp; 183 } 184 185 189 public LocationInfo getLocationInformation() { 190 if(locationInfo == null) { 191 locationInfo = new LocationInfo(new Throwable (), fqnOfCategoryClass); 192 } 193 return locationInfo; 194 } 195 196 199 public Level getLevel() { 200 return (Level) level; 201 } 202 203 207 public String getLoggerName() { 208 return categoryName; 209 } 210 211 220 public 221 Object getMessage() { 222 if(message != null) { 223 return message; 224 } else { 225 return getRenderedMessage(); 226 } 227 } 228 229 234 public 235 String getNDC() { 236 if(ndcLookupRequired) { 237 ndcLookupRequired = false; 238 ndc = NDC.get(); 239 } 240 return ndc; 241 } 242 243 244 256 public 257 Object getMDC(String key) { 258 Object r; 259 if(mdcCopy != null) { 262 r = mdcCopy.get(key); 263 if(r != null) { 264 return r; 265 } 266 } 267 return MDC.get(key); 268 } 269 270 274 public 275 void getMDCCopy() { 276 if(mdcCopyLookupRequired) { 277 mdcCopyLookupRequired = false; 278 Hashtable t = (Hashtable ) MDC.getContext(); 281 if(t != null) { 282 mdcCopy = (Hashtable ) t.clone(); 283 } 284 } 285 } 286 287 public 288 String getRenderedMessage() { 289 if(renderedMessage == null && message != null) { 290 if(message instanceof String ) 291 renderedMessage = (String ) message; 292 else { 293 LoggerRepository repository = logger.getLoggerRepository(); 294 295 if(repository instanceof RendererSupport) { 296 RendererSupport rs = (RendererSupport) repository; 297 renderedMessage= rs.getRendererMap().findAndRender(message); 298 } else { 299 renderedMessage = message.toString(); 300 } 301 } 302 } 303 return renderedMessage; 304 } 305 306 309 public static long getStartTime() { 310 return startTime; 311 } 312 313 public 314 String getThreadName() { 315 if(threadName == null) 316 threadName = (Thread.currentThread()).getName(); 317 return threadName; 318 } 319 320 328 public 329 ThrowableInformation getThrowableInformation() { 330 return throwableInfo; 331 } 332 333 336 public 337 String [] getThrowableStrRep() { 338 339 if(throwableInfo == null) 340 return null; 341 else 342 return throwableInfo.getThrowableStrRep(); 343 } 344 345 346 private 347 void readLevel(ObjectInputStream ois) 348 throws java.io.IOException , ClassNotFoundException { 349 350 int p = ois.readInt(); 351 try { 352 String className = (String ) ois.readObject(); 353 if(className == null) { 354 level = Level.toLevel(p); 355 } else { 356 Method m = (Method ) methodCache.get(className); 357 if(m == null) { 358 Class clazz = Loader.loadClass(className); 359 m = clazz.getDeclaredMethod(TO_LEVEL, TO_LEVEL_PARAMS); 366 methodCache.put(className, m); 367 } 368 PARAM_ARRAY[0] = new Integer (p); 369 level = (Level) m.invoke(null, PARAM_ARRAY); 370 } 371 } catch(Exception e) { 372 LogLog.warn("Level deserialization failed, reverting to default.", e); 373 level = Level.toLevel(p); 374 } 375 } 376 377 private void readObject(ObjectInputStream ois) 378 throws java.io.IOException , ClassNotFoundException { 379 ois.defaultReadObject(); 380 readLevel(ois); 381 382 if(locationInfo == null) 384 locationInfo = new LocationInfo(null, null); 385 } 386 387 private 388 void writeObject(ObjectOutputStream oos) throws java.io.IOException { 389 this.getThreadName(); 392 393 this.getRenderedMessage(); 395 396 this.getNDC(); 399 400 this.getMDCCopy(); 403 404 this.getThrowableStrRep(); 406 407 oos.defaultWriteObject(); 408 409 writeLevel(oos); 411 } 412 413 private 414 void writeLevel(ObjectOutputStream oos) throws java.io.IOException { 415 416 oos.writeInt(level.toInt()); 417 418 Class clazz = level.getClass(); 419 if(clazz == Level.class) { 420 oos.writeObject(null); 421 } else { 422 oos.writeObject(clazz.getName()); 426 } 427 } 428 429 } 430 | Popular Tags |