View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2011, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.html;
15  
16  import ch.qos.logback.classic.spi.ILoggingEvent;
17  import ch.qos.logback.classic.spi.IThrowableProxy;
18  import ch.qos.logback.classic.spi.StackTraceElementProxy;
19  import ch.qos.logback.core.CoreConstants;
20  import ch.qos.logback.core.helpers.Transform;
21  import ch.qos.logback.core.html.IThrowableRenderer;
22  
23  public class DefaultThrowableRenderer implements
24      IThrowableRenderer<ILoggingEvent> {
25  
26    static final String TRACE_PREFIX = "<br />&nbsp;&nbsp;&nbsp;&nbsp;";
27  
28    public DefaultThrowableRenderer() {
29    }
30  
31    public void render(StringBuilder sbuf, ILoggingEvent event) {
32      IThrowableProxy tp = event.getThrowableProxy();
33      sbuf.append("<tr><td class=\"Exception\" colspan=\"6\">");
34      while (tp != null) {
35        render(sbuf, tp);
36        tp = tp.getCause();
37      }
38      sbuf.append("</td></tr>");
39    }
40  
41    void render(StringBuilder sbuf, IThrowableProxy tp) {
42      printFirstLine(sbuf, tp);
43      
44      int commonFrames = tp.getCommonFrames();
45      StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
46      
47      for (int i = 0; i < stepArray.length - commonFrames; i++) {
48        StackTraceElementProxy step = stepArray[i];
49        sbuf.append(TRACE_PREFIX);
50        sbuf.append(Transform.escapeTags(step.toString()));
51        sbuf.append(CoreConstants.LINE_SEPARATOR);
52      }
53      
54      if (commonFrames > 0) {
55        sbuf.append(TRACE_PREFIX);
56        sbuf.append("\t... " + commonFrames).append(" common frames omitted")
57            .append(CoreConstants.LINE_SEPARATOR);
58      }
59    }
60  
61    public void printFirstLine(StringBuilder sb, IThrowableProxy tp) {
62      int commonFrames = tp.getCommonFrames();
63      if (commonFrames > 0) {
64        sb.append("<br />").append(CoreConstants.CAUSED_BY);
65      }
66      sb.append(tp.getClassName()).append(": ").append(
67          Transform.escapeTags(tp.getMessage()));
68      sb.append(CoreConstants.LINE_SEPARATOR);
69    }
70  
71  }