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