View Javadoc

1   /**
2    * Logback: the generic, reliable, fast and flexible logging framework.
3    * 
4    * Copyright (C) 2000-2008, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.classic.html;
11  
12  import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR;
13  import ch.qos.logback.classic.spi.LoggingEvent;
14  import ch.qos.logback.classic.spi.ThrowableDataPoint;
15  import ch.qos.logback.classic.spi.ThrowableProxy;
16  import ch.qos.logback.core.helpers.Transform;
17  import ch.qos.logback.core.html.IThrowableRenderer;
18  
19  public class DefaultThrowableRenderer implements IThrowableRenderer {
20    
21    static final String TRACE_PREFIX = "<br />&nbsp;&nbsp;&nbsp;&nbsp;";
22    
23    Throwable throwable;
24    
25    public DefaultThrowableRenderer() {
26      
27    }
28    
29    public void setThrowable(Throwable t) {
30      this.throwable = t;
31    }
32    
33    public void render(StringBuilder sbuf, ThrowableDataPoint[] tdpArray) {
34      if (tdpArray != null) {
35        int len = tdpArray.length;
36        if (len == 0) {
37          return;
38        }
39        sbuf.append("<tr><td class=\"Exception\" colspan=\"6\">");
40        sbuf.append(Transform.escapeTags(tdpArray[0].toString()));
41        sbuf.append(LINE_SEPARATOR);
42        for (int i = 1; i < len; i++) {
43          sbuf.append(TRACE_PREFIX);
44          sbuf.append(Transform.escapeTags(tdpArray[i].toString()));
45          sbuf.append(LINE_SEPARATOR);
46        }
47        sbuf.append("</td></tr>");
48      }
49    }
50    
51    public void render(StringBuilder sbuf, Object eventObject) {
52      LoggingEvent event = (LoggingEvent)eventObject;
53      ThrowableProxy tp = event.getThrowableProxy();
54      if (tp != null) {
55        render(sbuf, tp.getThrowableDataPointArray());
56      }
57    }
58  }