View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2009, 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 java.util.Map;
17  
18  import ch.qos.logback.classic.PatternLayout;
19  import ch.qos.logback.classic.spi.ILoggingEvent;
20  import ch.qos.logback.classic.html.DefaultCssBuilder;
21  import ch.qos.logback.core.html.HTMLLayoutBase;
22  import ch.qos.logback.core.html.IThrowableRenderer;
23  import ch.qos.logback.core.pattern.Converter;
24  import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR;
25  
26  /**
27   * 
28   * HTMLLayout outputs events in an HTML table. <p> The content of the table
29   * columns are specified using a conversion pattern. See
30   * {@link ch.qos.logback.classic.PatternLayout} for documentation on the
31   * available patterns. <p> For more information about this layout, please refer
32   * to the online manual at
33   * http://logback.qos.ch/manual/layouts.html#ClassicHTMLLayout
34   * 
35   * @author Ceki G&uuml;lc&uuml;
36   * @author S&eacute;bastien Pennec
37   */
38  public class HTMLLayout extends HTMLLayoutBase<ILoggingEvent> {
39  
40    /**
41     * Default pattern string for log output.
42     */
43    static final String DEFAULT_CONVERSION_PATTERN = "%date%thread%level%logger%mdc%msg";
44  
45    IThrowableRenderer<ILoggingEvent> throwableRenderer;
46  
47    /**
48     * Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN.
49     * 
50     * The default pattern just produces the application supplied message.
51     */
52    public HTMLLayout() {
53      pattern = DEFAULT_CONVERSION_PATTERN;
54      throwableRenderer = new DefaultThrowableRenderer();
55      cssBuilder = new DefaultCssBuilder();
56    }
57  
58    @Override
59    public void start() {
60      int errorCount = 0;
61      if (throwableRenderer == null) {
62        addError("ThrowableRender cannot be null.");
63        errorCount++;
64      }
65      if (errorCount == 0) {
66        super.start();
67      }
68    }
69  
70    protected Map<String, String> getDefaultConverterMap() {
71      return PatternLayout.defaultConverterMap;
72    }
73  
74    public String doLayout(ILoggingEvent event) {
75      StringBuilder buf = new StringBuilder();
76      startNewTableIfLimitReached(buf);
77  
78      boolean odd = true;
79      if (((counter++) & 1) == 0) {
80        odd = false;
81      }
82  
83      String level = event.getLevel().toString().toLowerCase();
84  
85      buf.append(LINE_SEPARATOR);
86      buf.append("<tr class=\"");
87      buf.append(level);
88      if (odd) {
89        buf.append(" odd\">");
90      } else {
91        buf.append(" even\">");
92      }
93      buf.append(LINE_SEPARATOR);
94  
95      Converter<ILoggingEvent> c = head;
96      while (c != null) {
97        appendEventToBuffer(buf, c, event);
98        c = c.getNext();
99      }
100     buf.append("</tr>");
101     buf.append(LINE_SEPARATOR);
102 
103     if (event.getThrowableProxy() != null) {
104       throwableRenderer.render(buf, event);
105     }
106     return buf.toString();
107   }
108 
109   private void appendEventToBuffer(StringBuilder buf,
110       Converter<ILoggingEvent> c, ILoggingEvent event) {
111     buf.append("<td class=\"");
112     buf.append(computeConverterName(c));
113     buf.append("\">");
114     buf.append(c.convert(event));
115     buf.append("</td>");
116     buf.append(LINE_SEPARATOR);
117   }
118 
119   public IThrowableRenderer getThrowableRenderer() {
120     return throwableRenderer;
121   }
122 
123   public void setThrowableRenderer(IThrowableRenderer<ILoggingEvent> throwableRenderer) {
124     this.throwableRenderer = throwableRenderer;
125   }
126 }