Changeset 1492

Show
Ignore:
Timestamp:
06/06/09 13:36:19 (3 years ago)
Author:
amandel
Message:

Add new way to get Loggable dump with nested parameters.

Location:
trunk/src/java/org/jcoderz/commons
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/java/org/jcoderz/commons/BaseException.java

    r1299 r1492  
    3737import java.util.List; 
    3838import java.util.Set; 
    39 import java.util.logging.Level; 
    4039import java.util.logging.Logger; 
    4140 
     
    199198   public String toString () 
    200199   { 
    201        return mLoggable.toString(); 
     200      return mLoggable.toString(); 
     201   } 
     202 
     203   /** {@inheritDoc} */ 
     204   public String toDetailedString () 
     205   { 
     206      return mLoggable.toDetailedString(); 
    202207   } 
    203208 
  • trunk/src/java/org/jcoderz/commons/BaseRuntimeException.java

    r1299 r1492  
    3737import java.util.List; 
    3838import java.util.Set; 
    39 import java.util.logging.Level; 
    4039import java.util.logging.Logger; 
    4140 
     
    198197   public String toString () 
    199198   { 
    200        return mLoggable.toString(); 
     199      return mLoggable.toString(); 
     200   } 
     201 
     202   /** {@inheritDoc} */ 
     203   public String toDetailedString () 
     204   { 
     205      return mLoggable.toDetailedString(); 
    201206   } 
    202207 
  • trunk/src/java/org/jcoderz/commons/LogEvent.java

    r1299 r1492  
    182182   } 
    183183 
     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 
    184197   LoggableImpl getExceptionImpl () 
    185198   { 
  • trunk/src/java/org/jcoderz/commons/Loggable.java

    r1299 r1492  
    170170 
    171171   /** 
     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   /** 
    172180    * The toString method of <code>Loggable</code> must dump out all 
    173181    * information stored within this loggable in a readable way. This 
     
    176184    *       <code>Loggable</code>. 
    177185    */ 
    178    String toString (); 
     186   String toDetailedString (); 
    179187 
    180188   /** 
  • trunk/src/java/org/jcoderz/commons/LoggableImpl.java

    r1299 r1492  
    3838import java.net.UnknownHostException; 
    3939import java.util.ArrayList; 
     40import java.util.Arrays; 
    4041import java.util.Collections; 
    4142import java.util.HashMap; 
     43import java.util.Iterator; 
    4244import java.util.List; 
    4345import java.util.Map; 
     
    182184   private final String mThreadName; 
    183185 
    184    /** The Throwable that caused this loggable. */ 
     186   /** 
     187    * The Throwable that caused this loggable. 
     188    * Should be equal to mOuter.getCause() 
     189    */ 
    185190   private Throwable mCause; 
    186191 
     
    196201   } 
    197202 
     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    */ 
    198209   public LoggableImpl (Loggable outer, LogMessageInfo errorId) 
    199210   { 
     
    202213   } 
    203214 
     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    */ 
    204222   public LoggableImpl (Loggable outer, LogMessageInfo errorId, 
    205223       Throwable cause) 
     
    210228   } 
    211229 
     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    */ 
    212240   public LoggableImpl (Loggable outer, LogMessageInfo errorId, 
    213241       long threadId, String threadName, String instanceId, String nodeId) 
     
    224252   } 
    225253 
     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    */ 
    226266   public LoggableImpl (Loggable outer, LogMessageInfo errorId, 
    227267       long threadId, String threadName, String instanceId, String nodeId, 
     
    229269   { 
    230270      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(); 
    234280      } 
    235281      else 
     
    396442      getLogMessageInfo().formatMessage(mParameters, sb); 
    397443      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(); 
    398486   } 
    399487 
     
    417505      if (mMethodName == null || mClassName == null) 
    418506      { 
    419           final StackTraceElement[] stack = (new Throwable()).getStackTrace(); 
     507          final StackTraceElement[] stack = new Throwable().getStackTrace(); 
    420508          // First, search back to a method in the Logger class. 
    421509          int ix = 0; 
     
    486574   } 
    487575 
     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 
    488594   private static String getStaticNodeId () 
    489595   { 
     
    523629      } 
    524630   } 
     631 
     632 
    525633} 
  • trunk/src/java/org/jcoderz/commons/util/ThrowableUtil.java

    r1011 r1492  
    3333package org.jcoderz.commons.util; 
    3434 
     35import java.io.PrintWriter; 
     36import java.io.StringWriter; 
    3537import java.lang.reflect.Method; 
    3638import java.lang.reflect.Modifier; 
     
    4749public final class ThrowableUtil 
    4850{ 
    49    /**  
     51   /** 
    5052    * 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. 
    5254    */ 
    5355   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 
    5658    * is available. 
    57     * This should be the case for all JDKs > 1.4.  
     59    * This should be the case for all JDKs > 1.4. 
    5860    */ 
    5961   private static final Method INIT_CAUSE; 
     
    99101    * classes and pass the nested exceptions into the standart 
    100102    * 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 
    102104    * give exception. 
    103105    * @param ex the exception to be checked. 
     
    139141   } 
    140142 
     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 
    141166   static Method findGetCauseMethod (final Method[] methods) 
    142167   {