View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, 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 IThrowableRenderer<ILoggingEvent> {
24  
25      static final String TRACE_PREFIX = "<br />&nbsp;&nbsp;&nbsp;&nbsp;";
26  
27      public void render(StringBuilder sbuf, ILoggingEvent event) {
28          IThrowableProxy tp = event.getThrowableProxy();
29          sbuf.append("<tr><td class=\"Exception\" colspan=\"6\">");
30          while (tp != null) {
31              render(sbuf, tp);
32              tp = tp.getCause();
33          }
34          sbuf.append("</td></tr>");
35      }
36  
37      void render(StringBuilder sbuf, IThrowableProxy tp) {
38          printFirstLine(sbuf, tp);
39  
40          int commonFrames = tp.getCommonFrames();
41          StackTraceElementProxy[] stepArray = tp.getStackTraceElementProxyArray();
42  
43          for (int i = 0; i < stepArray.length - commonFrames; i++) {
44              StackTraceElementProxy step = stepArray[i];
45              sbuf.append(TRACE_PREFIX);
46              sbuf.append(Transform.escapeTags(step.toString()));
47              sbuf.append(CoreConstants.LINE_SEPARATOR);
48          }
49  
50          if (commonFrames > 0) {
51              sbuf.append(TRACE_PREFIX);
52              sbuf.append("\t... ").append(commonFrames).append(" common frames omitted")
53                      .append(CoreConstants.LINE_SEPARATOR);
54          }
55      }
56  
57      public void printFirstLine(StringBuilder sb, IThrowableProxy tp) {
58          int commonFrames = tp.getCommonFrames();
59          if (commonFrames > 0) {
60              sb.append("<br />").append(CoreConstants.CAUSED_BY);
61          }
62          sb.append(tp.getClassName()).append(": ").append(Transform.escapeTags(tp.getMessage()));
63          sb.append(CoreConstants.LINE_SEPARATOR);
64      }
65  
66  }