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.QuoteMode;
024import org.apache.logging.log4j.core.Layout;
025import org.apache.logging.log4j.core.LogEvent;
026import org.apache.logging.log4j.core.config.Configuration;
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.PluginConfiguration;
031import org.apache.logging.log4j.core.config.plugins.PluginFactory;
032import org.apache.logging.log4j.message.Message;
033import org.apache.logging.log4j.status.StatusLogger;
034
035/**
036 * A Comma-Separated Value (CSV) layout to log event parameters.
037 * The event message is currently ignored.
038 *
039 * <p>
040 * Best used with:
041 * </p>
042 * <p>
043 * {@code logger.debug(new ObjectArrayMessage(1, 2, "Bob"));}
044 * </p>
045 *
046 * Depends on Apache Commons CSV 1.4.
047 *
048 * @since 2.4
049 */
050@Plugin(name = "CsvParameterLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
051public class CsvParameterLayout extends AbstractCsvLayout {
052
053    public static AbstractCsvLayout createDefaultLayout() {
054        return new CsvParameterLayout(null, Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
055    }
056
057    public static AbstractCsvLayout createLayout(final CSVFormat format) {
058        return new CsvParameterLayout(null, Charset.forName(DEFAULT_CHARSET), format, null, null);
059    }
060
061    @PluginFactory
062    public static AbstractCsvLayout createLayout(
063            // @formatter:off
064            @PluginConfiguration final Configuration config,
065            @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format,
066            @PluginAttribute("delimiter") final Character delimiter,
067            @PluginAttribute("escape") final Character escape,
068            @PluginAttribute("quote") final Character quote,
069            @PluginAttribute("quoteMode") final QuoteMode quoteMode,
070            @PluginAttribute("nullString") final String nullString,
071            @PluginAttribute("recordSeparator") final String recordSeparator,
072            @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset,
073            @PluginAttribute("header") final String header,
074            @PluginAttribute("footer") final String footer)
075            // @formatter:on
076    {
077
078        final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
079        return new CsvParameterLayout(config, charset, csvFormat, header, footer);
080    }
081
082    public CsvParameterLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
083        super(config, charset, csvFormat, header, footer);
084    }
085
086    @Override
087    public String toSerializable(final LogEvent event) {
088        final Message message = event.getMessage();
089        final Object[] parameters = message.getParameters();
090        final StringBuilder buffer = getStringBuilder();
091        try {
092            getFormat().printRecord(buffer, parameters);
093            return buffer.toString();
094        } catch (final IOException e) {
095            StatusLogger.getLogger().error(message, e);
096            return getFormat().getCommentMarker() + " " + e;
097        }
098    }
099
100}