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