View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.layout;
18  
19  import java.io.IOException;
20  import java.nio.charset.Charset;
21  
22  import org.apache.commons.csv.CSVFormat;
23  import org.apache.commons.csv.CSVPrinter;
24  import org.apache.commons.csv.QuoteMode;
25  import org.apache.logging.log4j.core.Layout;
26  import org.apache.logging.log4j.core.LogEvent;
27  import org.apache.logging.log4j.core.config.Configuration;
28  import org.apache.logging.log4j.core.config.Node;
29  import org.apache.logging.log4j.core.config.plugins.Plugin;
30  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
31  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
32  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
33  import org.apache.logging.log4j.message.Message;
34  import org.apache.logging.log4j.status.StatusLogger;
35  
36  /**
37   * A Comma-Separated Value (CSV) layout to log event parameters.
38   * The event message is currently ignored. 
39   * 
40   * <p>
41   * Best used with:
42   * </p>
43   * <p>
44   * {@code logger.debug(new ObjectArrayMessage(1, 2, "Bob"));}
45   * </p>
46   * 
47   * Depends on Apache Commons CSV 1.2.
48   * 
49   * @since 2.4
50   */
51  @Plugin(name = "CsvParameterLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
52  public class CsvParameterLayout extends AbstractCsvLayout {
53  
54      public static AbstractCsvLayout createDefaultLayout() {
55          return new CsvParameterLayout(null, Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
56      }
57  
58      public static AbstractCsvLayout createLayout(final CSVFormat format) {
59          return new CsvParameterLayout(null, Charset.forName(DEFAULT_CHARSET), format, null, null);
60      }
61  
62      @PluginFactory
63      public static AbstractCsvLayout createLayout(
64              // @formatter:off
65              @PluginConfiguration final Configuration config,
66              @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format,
67              @PluginAttribute("delimiter") final Character delimiter,
68              @PluginAttribute("escape") final Character escape,
69              @PluginAttribute("quote") final Character quote,
70              @PluginAttribute("quoteMode") final QuoteMode quoteMode,
71              @PluginAttribute("nullString") final String nullString,
72              @PluginAttribute("recordSeparator") final String recordSeparator,
73              @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset,
74              @PluginAttribute("header") final String header, 
75              @PluginAttribute("footer") final String footer)
76              // @formatter:on
77      {
78  
79          final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
80          return new CsvParameterLayout(config, charset, csvFormat, header, footer);
81      }
82  
83      public CsvParameterLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
84          super(config, charset, csvFormat, header, footer);
85      }
86  
87      @Override
88      public String toSerializable(final LogEvent event) {
89          final Message message = event.getMessage();
90          final Object[] parameters = message.getParameters();
91          final StringBuilder buffer = getStringBuilder();
92          try {
93              getFormat().printRecord(buffer, parameters);
94              return buffer.toString();
95          } catch (final IOException e) {
96              StatusLogger.getLogger().error(message, e);
97              return getFormat().getCommentMarker() + " " + e;
98          }
99      }
100 
101 }