Changeset 1492
- Timestamp:
- 06/06/09 13:36:19 (3 years ago)
- Location:
- trunk/src/java/org/jcoderz/commons
- Files:
-
- 6 modified
-
BaseException.java (modified) (2 diffs)
-
BaseRuntimeException.java (modified) (2 diffs)
-
LogEvent.java (modified) (1 diff)
-
Loggable.java (modified) (2 diffs)
-
LoggableImpl.java (modified) (11 diffs)
-
util/ThrowableUtil.java (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/java/org/jcoderz/commons/BaseException.java
r1299 r1492 37 37 import java.util.List; 38 38 import java.util.Set; 39 import java.util.logging.Level;40 39 import java.util.logging.Logger; 41 40 … … 199 198 public String toString () 200 199 { 201 return mLoggable.toString(); 200 return mLoggable.toString(); 201 } 202 203 /** {@inheritDoc} */ 204 public String toDetailedString () 205 { 206 return mLoggable.toDetailedString(); 202 207 } 203 208 -
trunk/src/java/org/jcoderz/commons/BaseRuntimeException.java
r1299 r1492 37 37 import java.util.List; 38 38 import java.util.Set; 39 import java.util.logging.Level;40 39 import java.util.logging.Logger; 41 40 … … 198 197 public String toString () 199 198 { 200 return mLoggable.toString(); 199 return mLoggable.toString(); 200 } 201 202 /** {@inheritDoc} */ 203 public String toDetailedString () 204 { 205 return mLoggable.toDetailedString(); 201 206 } 202 207 -
trunk/src/java/org/jcoderz/commons/LogEvent.java
r1299 r1492 182 182 } 183 183 184 /** {@inheritDoc} */ 185 public String toString () 186 { 187 return mLoggable.toString(); 188 } 189 190 /** {@inheritDoc} */ 191 public String toDetailedString () 192 { 193 return mLoggable.toDetailedString(); 194 } 195 196 184 197 LoggableImpl getExceptionImpl () 185 198 { -
trunk/src/java/org/jcoderz/commons/Loggable.java
r1299 r1492 170 170 171 171 /** 172 * The toString method of <code>Loggable</code> dumps the 173 * String representation of the class name of the loggable 174 * and the contained message. 175 * @return one line information about this <code>Loggable</code>. 176 */ 177 String toString (); 178 179 /** 172 180 * The toString method of <code>Loggable</code> must dump out all 173 181 * information stored within this loggable in a readable way. This … … 176 184 * <code>Loggable</code>. 177 185 */ 178 String to String ();186 String toDetailedString (); 179 187 180 188 /** -
trunk/src/java/org/jcoderz/commons/LoggableImpl.java
r1299 r1492 38 38 import java.net.UnknownHostException; 39 39 import java.util.ArrayList; 40 import java.util.Arrays; 40 41 import java.util.Collections; 41 42 import java.util.HashMap; 43 import java.util.Iterator; 42 44 import java.util.List; 43 45 import java.util.Map; … … 182 184 private final String mThreadName; 183 185 184 /** The Throwable that caused this loggable. */ 186 /** 187 * The Throwable that caused this loggable. 188 * Should be equal to mOuter.getCause() 189 */ 185 190 private Throwable mCause; 186 191 … … 196 201 } 197 202 203 /** 204 * Create this loggable provide the 'Loggable' functionality for the 205 * given outer loggable. 206 * @param outer the the outer loggable. 207 * @param errorId the static LogMessageInfo for this Loggable. 208 */ 198 209 public LoggableImpl (Loggable outer, LogMessageInfo errorId) 199 210 { … … 202 213 } 203 214 215 /** 216 * Create this loggable provide the 'Loggable' functionality for the 217 * given outer loggable with an initial cause. 218 * @param outer the the outer loggable. 219 * @param errorId the static LogMessageInfo for this Loggable. 220 * @param cause the cause of the outer. 221 */ 204 222 public LoggableImpl (Loggable outer, LogMessageInfo errorId, 205 223 Throwable cause) … … 210 228 } 211 229 230 /** 231 * Create this loggable provide the 'Loggable' functionality for the 232 * given outer loggable with the given dynamic parameters. 233 * @param outer the the outer loggable. 234 * @param errorId the static LogMessageInfo for this Loggable. 235 * @param threadId the threadId to be set. 236 * @param threadName the threadName to be set. 237 * @param instanceId the instanceId to be set. 238 * @param nodeId the nodeId to be set. 239 */ 212 240 public LoggableImpl (Loggable outer, LogMessageInfo errorId, 213 241 long threadId, String threadName, String instanceId, String nodeId) … … 224 252 } 225 253 254 /** 255 * Create this loggable provide the 'Loggable' functionality for the 256 * given outer loggable with the given dynamic parameters and an 257 * initial cause.. 258 * @param outer the the outer loggable. 259 * @param errorId the static LogMessageInfo for this Loggable. 260 * @param threadId the threadId to be set. 261 * @param threadName the threadName to be set. 262 * @param instanceId the instanceId to be set. 263 * @param nodeId the nodeId to be set. 264 * @param cause the cause of the outer. 265 */ 226 266 public LoggableImpl (Loggable outer, LogMessageInfo errorId, 227 267 long threadId, String threadName, String instanceId, String nodeId, … … 229 269 { 230 270 mEventTime = System.currentTimeMillis(); 231 if (cause instanceof Loggable) 232 { 233 mTrackingNumber = ((Loggable) cause).getTrackingNumber(); 271 ThrowableUtil.fixChaining(cause); 272 Throwable thr = cause; 273 while (thr != null && !(thr instanceof Loggable)) 274 { 275 thr = thr.getCause(); 276 } 277 if (thr instanceof Loggable) 278 { 279 mTrackingNumber = ((Loggable) thr).getTrackingNumber(); 234 280 } 235 281 else … … 396 442 getLogMessageInfo().formatMessage(mParameters, sb); 397 443 return sb.toString(); 444 } 445 446 /** {@inheritDoc} */ 447 public String toDetailedString () 448 { 449 final StringBuffer sb = new StringBuffer(); 450 LoggableImpl.appendParameters(sb, this); 451 Throwable cause = null; 452 if (mOuter != null) 453 { 454 cause = mOuter.getCause(); 455 } 456 if (cause == null) 457 { 458 cause = getCause(); 459 } 460 // add parameters of nested chain 461 while (cause != null) 462 { 463 if (cause instanceof Loggable) 464 { 465 sb.append("\nCaused by: "); 466 LoggableImpl.appendParameters(sb, (Loggable) cause); 467 break; 468 } 469 cause = cause.getCause(); 470 } 471 cause = null; 472 if (mOuter != null) 473 { 474 cause = mOuter.getCause(); 475 } 476 if (cause == null) 477 { 478 cause = getCause(); 479 } 480 if (cause != null) 481 { 482 sb.append('\n'); 483 sb.append(ThrowableUtil.toString(cause)); 484 } 485 return sb.toString(); 398 486 } 399 487 … … 417 505 if (mMethodName == null || mClassName == null) 418 506 { 419 final StackTraceElement[] stack = (new Throwable()).getStackTrace();507 final StackTraceElement[] stack = new Throwable().getStackTrace(); 420 508 // First, search back to a method in the Logger class. 421 509 int ix = 0; … … 486 574 } 487 575 576 private static void appendParameters (StringBuffer sb, Loggable loggable) 577 { 578 sb.append(loggable.toString()); 579 580 final Object[] params = loggable.getParameterNames().toArray(); 581 Arrays.sort(params); 582 final Iterator/*<String>*/ parameterNames 583 = Arrays.asList(params).iterator(); 584 while(parameterNames.hasNext()) 585 { 586 final String parameterName = (String) parameterNames.next(); 587 sb.append("\n\t"); 588 sb.append(parameterName); 589 sb.append(": \t"); 590 sb.append(loggable.getParameter(parameterName)); 591 } 592 } 593 488 594 private static String getStaticNodeId () 489 595 { … … 523 629 } 524 630 } 631 632 525 633 } -
trunk/src/java/org/jcoderz/commons/util/ThrowableUtil.java
r1011 r1492 33 33 package org.jcoderz.commons.util; 34 34 35 import java.io.PrintWriter; 36 import java.io.StringWriter; 35 37 import java.lang.reflect.Method; 36 38 import java.lang.reflect.Modifier; … … 47 49 public final class ThrowableUtil 48 50 { 49 /** 51 /** 50 52 * Stores the Throwable.getCause() method if this method is available. 51 * This should be the case for all JDKs > 1.4. 53 * This should be the case for all JDKs > 1.4. 52 54 */ 53 55 private static final Method GET_CAUSE; 54 /** 55 * Stores the Throwable.initCause(Throwable) method if this method 56 /** 57 * Stores the Throwable.initCause(Throwable) method if this method 56 58 * is available. 57 * This should be the case for all JDKs > 1.4. 59 * This should be the case for all JDKs > 1.4. 58 60 */ 59 61 private static final Method INIT_CAUSE; … … 99 101 * classes and pass the nested exceptions into the standart 100 102 * nesting mechanism available since JDK1.4 with the throwable 101 * class. It is save to call this method several times for a 103 * class. It is save to call this method several times for a 102 104 * give exception. 103 105 * @param ex the exception to be checked. … … 139 141 } 140 142 143 /** 144 * Dumps the stack trace of the given throwable to its String representation. 145 * @param thr the throwable to dump the stack trace from. 146 * @return a String representation of the given throwable 147 * @see Throwable#printStackTrace() 148 */ 149 public static String toString (Throwable thr) 150 { 151 final StringWriter sw = new StringWriter(); 152 PrintWriter pw = null; 153 try 154 { 155 pw = new PrintWriter(sw); 156 thr.printStackTrace(pw); 157 } 158 finally 159 { 160 IoUtil.close(pw); 161 IoUtil.close(sw); 162 } 163 return sw.toString(); 164 } 165 141 166 static Method findGetCauseMethod (final Method[] methods) 142 167 {
