001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.layout;
018
019import java.io.IOException;
020import java.nio.charset.Charset;
021
022import org.apache.commons.csv.CSVFormat;
023import org.apache.commons.csv.CSVPrinter;
024import org.apache.commons.csv.QuoteMode;
025import org.apache.logging.log4j.core.Layout;
026import org.apache.logging.log4j.core.LogEvent;
027import org.apache.logging.log4j.core.config.Node;
028import org.apache.logging.log4j.core.config.plugins.Plugin;
029import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
030import org.apache.logging.log4j.core.config.plugins.PluginFactory;
031import org.apache.logging.log4j.status.StatusLogger;
032
033/**
034 * A Comma-Separated Value (CSV) layout to log events.
035 * 
036 * Depends on Apache Commons CSV 1.2.
037 * 
038 * @since 2.4
039 */
040@Plugin(name = "CsvLogEventLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
041public class CsvLogEventLayout extends AbstractCsvLayout {
042
043    /**
044     * 
045     */
046    private static final long serialVersionUID = 1L;
047
048    public static CsvLogEventLayout createDefaultLayout() {
049        return new CsvLogEventLayout(Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
050    }
051
052    public static CsvLogEventLayout createLayout(final CSVFormat format) {
053        return new CsvLogEventLayout(Charset.forName(DEFAULT_CHARSET), format, null, null);
054    }
055
056    @PluginFactory
057    public static CsvLogEventLayout createLayout(
058            // @formatter:off
059            @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format,
060            @PluginAttribute("delimiter") final Character delimiter,
061            @PluginAttribute("escape") final Character escape,
062            @PluginAttribute("quote") final Character quote,
063            @PluginAttribute("quoteMode") final QuoteMode quoteMode,
064            @PluginAttribute("nullString") final String nullString,
065            @PluginAttribute("recordSeparator") final String recordSeparator,
066            @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset,
067            @PluginAttribute("header") final String header,
068            @PluginAttribute("footer") final String footer)
069            // @formatter:on
070    {
071
072        final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
073        return new CsvLogEventLayout(charset, csvFormat, header, footer);
074    }
075   
076    protected CsvLogEventLayout(final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
077        super(charset, csvFormat, header, footer);
078    }
079
080    @Override
081    public String toSerializable(final LogEvent event) {
082        final StringBuilder buffer = new StringBuilder(1024);
083        try {
084            // Revisit when 1.3 is out so that we do not need to create a new
085            // printer for each event.
086            // No need to close the printer.
087            final CSVPrinter printer = new CSVPrinter(buffer, getFormat());
088            printer.print(event.getNanoTime());
089            printer.print(event.getTimeMillis());
090            printer.print(event.getLevel());
091            printer.print(event.getThreadName());
092            printer.print(event.getMessage().getFormattedMessage());
093            printer.print(event.getLoggerFqcn());
094            printer.print(event.getLoggerName());
095            printer.print(event.getMarker());
096            printer.print(event.getThrownProxy());
097            printer.print(event.getSource());
098            printer.print(event.getContextMap());
099            printer.print(event.getContextStack());
100            printer.println();
101            return buffer.toString();
102        } catch (final IOException e) {
103            StatusLogger.getLogger().error(event.toString(), e);
104            return getFormat().getCommentMarker() + " " + e;
105        }
106    }
107
108}