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.access.html;
15  
16  import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR;
17  
18  import java.util.Map;
19  
20  import ch.qos.logback.access.PatternLayout;
21  import ch.qos.logback.access.spi.IAccessEvent;
22  import ch.qos.logback.core.html.HTMLLayoutBase;
23  import ch.qos.logback.core.pattern.Converter;
24  
25  /**
26   * 
27   * HTMLLayout outputs events in an HTML table. 
28   * <p>
29   * The content of the table columns are specified using a conversion pattern. 
30   * See {@link ch.qos.logback.access.PatternLayout} for documentation on the
31   * available patterns.
32   * <p>
33   * For more information about this layout, please refer to the online manual at
34   * http://logback.qos.ch/manual/layouts.html#AccessHTMLLayout
35   * 
36   * 
37   * @author Ceki G&uuml;lc&uuml;
38   * @author S&eacute;bastien Pennec
39   */
40  public class HTMLLayout extends HTMLLayoutBase<IAccessEvent> {
41  
42    /**
43     * Default pattern string for log output.
44     */
45    static final String DEFAULT_CONVERSION_PATTERN = "%h%l%u%t%r%s%b";
46  
47    /**
48     * Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN.
49     * 
50     */
51    public HTMLLayout() {
52      pattern = DEFAULT_CONVERSION_PATTERN;
53      cssBuilder = new DefaultCssBuilder();  
54    }
55    
56    @Override
57    protected Map<String, String> getDefaultConverterMap() {
58      return PatternLayout.defaultConverterMap;
59    }
60  
61    public String doLayout(IAccessEvent event) {
62      StringBuilder buf = new StringBuilder();
63      startNewTableIfLimitReached(buf);
64  
65      boolean odd = true;
66      if (((counter++) & 1) == 0) {
67        odd = false;
68      }
69  
70      buf.append(LINE_SEPARATOR);
71      buf.append("<tr class=\"");
72      if (odd) {
73        buf.append(" odd\">");
74      } else {
75        buf.append(" even\">");
76      }
77      buf.append(LINE_SEPARATOR);
78  
79      Converter<IAccessEvent> c = head;
80      while (c != null) {
81        appendEventToBuffer(buf, c, event);
82        c = c.getNext();
83      }
84      buf.append("</tr>");
85      buf.append(LINE_SEPARATOR);
86  
87      return buf.toString();
88    }
89  
90    private void appendEventToBuffer(StringBuilder buf, Converter<IAccessEvent> c,
91        IAccessEvent event) {
92      buf.append("<td class=\"");
93      buf.append(computeConverterName(c));
94      buf.append("\">");
95      c.write(buf, event);
96      buf.append("</td>");
97      buf.append(LINE_SEPARATOR);
98    }
99  }