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.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      @Override
62      public String doLayout(IAccessEvent event) {
63          StringBuilder buf = new StringBuilder();
64          startNewTableIfLimitReached(buf);
65  
66          boolean odd = true;
67          if (((counter++) & 1) == 0) {
68              odd = false;
69          }
70  
71          buf.append(LINE_SEPARATOR);
72          buf.append("<tr class=\"");
73          if (odd) {
74              buf.append(" odd\">");
75          } else {
76              buf.append(" even\">");
77          }
78          buf.append(LINE_SEPARATOR);
79  
80          Converter<IAccessEvent> c = head;
81          while (c != null) {
82              appendEventToBuffer(buf, c, event);
83              c = c.getNext();
84          }
85          buf.append("</tr>");
86          buf.append(LINE_SEPARATOR);
87  
88          return buf.toString();
89      }
90  
91      private void appendEventToBuffer(StringBuilder buf, Converter<IAccessEvent> c, 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  }