View Javadoc

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