001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.common.html;
015
016import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR;
017
018import java.util.Map;
019import java.util.function.Supplier;
020
021import ch.qos.logback.access.common.PatternLayout;
022import ch.qos.logback.access.common.spi.IAccessEvent;
023import ch.qos.logback.core.html.HTMLLayoutBase;
024import ch.qos.logback.core.pattern.Converter;
025import ch.qos.logback.core.pattern.DynamicConverter;
026
027/**
028 * 
029 * HTMLLayout outputs events in an HTML table.
030 * <p>
031 * The content of the table columns are specified using a conversion pattern.
032 * See {@link PatternLayout} for documentation on the
033 * available patterns.
034 * <p>
035 * For more information about this layout, please refer to the online manual at
036 * http://logback.qos.ch/manual/layouts.html#AccessHTMLLayout
037 * 
038 * 
039 * @author Ceki G&uuml;lc&uuml;
040 * @author S&eacute;bastien Pennec
041 */
042public class HTMLLayout extends HTMLLayoutBase<IAccessEvent> {
043
044    /**
045     * Default pattern string for log output.
046     */
047    static final String DEFAULT_CONVERSION_PATTERN = "%h%l%u%t%r%s%b";
048
049    /**
050     * Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN.
051     * 
052     */
053    public HTMLLayout() {
054        pattern = DEFAULT_CONVERSION_PATTERN;
055        cssBuilder = new DefaultCssBuilder();
056    }
057
058    @Override
059    protected Map<String, Supplier<DynamicConverter>> getDefaultConverterSupplierMap() {
060        return PatternLayout.ACCESS_DEFAULT_CONVERTER_SUPPLIER_MAP;
061    }
062
063    @Override
064    public String doLayout(IAccessEvent event) {
065        StringBuilder buf = new StringBuilder();
066        startNewTableIfLimitReached(buf);
067
068        boolean odd = true;
069        if (((counter++) & 1) == 0) {
070            odd = false;
071        }
072
073        buf.append(LINE_SEPARATOR);
074        buf.append("<tr class=\"");
075        if (odd) {
076            buf.append(" odd\">");
077        } else {
078            buf.append(" even\">");
079        }
080        buf.append(LINE_SEPARATOR);
081
082        Converter<IAccessEvent> c = head;
083        while (c != null) {
084            appendEventToBuffer(buf, c, event);
085            c = c.getNext();
086        }
087        buf.append("</tr>");
088        buf.append(LINE_SEPARATOR);
089
090        return buf.toString();
091    }
092
093    private void appendEventToBuffer(StringBuilder buf, Converter<IAccessEvent> c, IAccessEvent event) {
094        buf.append("<td class=\"");
095        buf.append(computeConverterName(c));
096        buf.append("\">");
097        c.write(buf, event);
098        buf.append("</td>");
099        buf.append(LINE_SEPARATOR);
100    }
101}