View | Details | Raw Unified | Return to bug 48902
Collapse All | Expand All

(-)src/main/java/org/apache/log4j/EnhancedPatternLayout.java (-3 / +4 lines)
Lines 280-288 Link Here
280
     <p>Used to output the Throwable trace that has been bound to the LoggingEvent, by
280
     <p>Used to output the Throwable trace that has been bound to the LoggingEvent, by
281
     default this will output the full trace as one would normally find by a call to Throwable.printStackTrace().
281
     default this will output the full trace as one would normally find by a call to Throwable.printStackTrace().
282
     The throwable conversion word can be followed by an option in the form <b>%throwable{short}</b>
282
     The throwable conversion word can be followed by an option in the form <b>%throwable{short}</b>
283
     which will only output the first line of the ThrowableInformation, or <b>%throwable{none}</b> which
283
     which will only output the first line of the ThrowableInformation, <b>%throwable{compact}</b> which will 
284
     will suppress the stack trace.  If no %throwable pattern is provided, the appender may provide its
284
     concatenate on the log mesage line the class name and the message of the throwable and its causes, or 
285
     rendering of the exception.</p>
285
     <b>%throwable{none}</b> which will suppress the stack trace.  If no %throwable pattern is provided, 
286
     the appender may provide its rendering of the exception.</p>
286
     </td>
287
     </td>
287
   </tr>
288
   </tr>
288
289
(-)src/main/java/org/apache/log4j/pattern/ThrowableInformationPatternConverter.java (-12 / +55 lines)
Lines 17-22 Link Here
17
17
18
package org.apache.log4j.pattern;
18
package org.apache.log4j.pattern;
19
19
20
import java.util.ArrayList;
21
import java.util.Iterator;
22
import java.util.List;
23
20
import org.apache.log4j.spi.LoggingEvent;
24
import org.apache.log4j.spi.LoggingEvent;
21
import org.apache.log4j.spi.ThrowableInformation;
25
import org.apache.log4j.spi.ThrowableInformation;
22
26
Lines 32-37 Link Here
32
  extends LoggingEventPatternConverter {
36
  extends LoggingEventPatternConverter {
33
  /**
37
  /**
34
   * If "short", only first line of throwable report will be formatted.
38
   * If "short", only first line of throwable report will be formatted.
39
   * If "compact", the class name and the message of the throwable and its causes
40
   * are concatenated and emitted in the logs.
35
   */
41
   */
36
  private final String option;
42
  private final String option;
37
43
Lines 65-90 Link Here
65
   * {@inheritDoc}
71
   * {@inheritDoc}
66
   */
72
   */
67
  public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
73
  public void format(final LoggingEvent event, final StringBuffer toAppendTo) {
68
    if (!"none".equals(option)) {
74
    ThrowableInformation information = event.getThrowableInformation();
69
      ThrowableInformation information = event.getThrowableInformation();
70
75
71
      if (information != null) {
76
    if (information == null) {
72
        String[] stringRep = information.getThrowableStrRep();
77
      return;
78
    }
79
          
80
    if ("none".equals(option)) {
81
      // nothing
73
82
74
        int length = stringRep.length;
83
    } else if ("short".equals(option)) {
75
        if ("short".equals(option)) {
84
      // toString() of the throwable
76
           length = 1;
85
      String msg = information.getThrowableStrRep()[0];
86
      toAppendTo.append(msg).append("\n");
87
 
88
    } else if ("compact".equals(option)){
89
      // join on the same line class name and message of the throwable and its causes
90
      StringBuilder msg = new StringBuilder(" - ");
91
      List throwables = getThrowableList(information.getThrowable());
92
      for (Iterator it = throwables.iterator(); it.hasNext();) {
93
        Throwable throwable = (Throwable)it.next();
94
        msg.append(throwable.getClass().getName()).append(": ")
95
           .append(throwable.getMessage());
96
        if (it.hasNext()) {
97
          msg.append(" ");
77
        }
98
        }
78
79
        for (int i = 0; i < length; i++) {
80
            String string = stringRep[i];
81
            toAppendTo.append(string).append("\n");
82
        }
83
      }
99
      }
100
      toAppendTo.append(msg);
101
  
102
    } else {
103
      // each line of the stack trace
104
      String[] stringRep = information.getThrowableStrRep();
105
      for (int i = 0; i < stringRep.length; i++) {
106
        String string = stringRep[i];
107
        toAppendTo.append(string).append("\n");
108
      }
84
    }
109
    }
85
  }
110
  }
86
111
87
  /**
112
  /**
113
   * Builds the list of the given <tt>throwable</tt> and all its causes.
114
   * 
115
   * <p>Only relies on {@link Throwable#getCause()}.</p>
116
   * 
117
   * @param throwable
118
   * @return non <tt>null<tt> list of non <tt>null</tt> throwables
119
   * @see Throwable#getCause()
120
   */
121
  public List getThrowableList(Throwable throwable) {
122
    List throwables = new ArrayList();
123
    while (throwable != null && throwables.contains(throwable) == false) {
124
      throwables.add(throwable);
125
      throwable = throwable.getCause();
126
    }
127
    return throwables;
128
  }
129
  
130
  /**
88
   * This converter obviously handles throwables.
131
   * This converter obviously handles throwables.
89
   * @return true.
132
   * @return true.
90
   */
133
   */

Return to bug 48902