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.QuoteMode;
24  import org.apache.logging.log4j.core.Layout;
25  import org.apache.logging.log4j.core.LogEvent;
26  import org.apache.logging.log4j.core.config.Configuration;
27  import org.apache.logging.log4j.core.config.Node;
28  import org.apache.logging.log4j.core.config.plugins.Plugin;
29  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
30  import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
31  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
32  import org.apache.logging.log4j.message.Message;
33  import org.apache.logging.log4j.status.StatusLogger;
34  
35  /**
36   * A Comma-Separated Value (CSV) layout to log event parameters.
37   * The event message is currently ignored.
38   *
39   * <p>
40   * Best used with:
41   * </p>
42   * <p>
43   * {@code logger.debug(new ObjectArrayMessage(1, 2, "Bob"));}
44   * </p>
45   *
46   * Depends on Apache Commons CSV 1.4.
47   *
48   * @since 2.4
49   */
50  @Plugin(name = "CsvParameterLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
51  public class CsvParameterLayout extends AbstractCsvLayout {
52  
53      public static AbstractCsvLayout createDefaultLayout() {
54          return new CsvParameterLayout(null, Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
55      }
56  
57      public static AbstractCsvLayout createLayout(final CSVFormat format) {
58          return new CsvParameterLayout(null, Charset.forName(DEFAULT_CHARSET), format, null, null);
59      }
60  
61      @PluginFactory
62      public static AbstractCsvLayout createLayout(
63              // @formatter:off
64              @PluginConfiguration final Configuration config,
65              @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format,
66              @PluginAttribute("delimiter") final Character delimiter,
67              @PluginAttribute("escape") final Character escape,
68              @PluginAttribute("quote") final Character quote,
69              @PluginAttribute("quoteMode") final QuoteMode quoteMode,
70              @PluginAttribute("nullString") final String nullString,
71              @PluginAttribute("recordSeparator") final String recordSeparator,
72              @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset,
73              @PluginAttribute("header") final String header,
74              @PluginAttribute("footer") final String footer)
75              // @formatter:on
76      {
77  
78          final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
79          return new CsvParameterLayout(config, charset, csvFormat, header, footer);
80      }
81  
82      public CsvParameterLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
83          super(config, charset, csvFormat, header, footer);
84      }
85  
86      @Override
87      public String toSerializable(final LogEvent event) {
88          final Message message = event.getMessage();
89          final Object[] parameters = message.getParameters();
90          final StringBuilder buffer = getStringBuilder();
91          try {
92              getFormat().printRecord(buffer, parameters);
93              return buffer.toString();
94          } catch (final IOException e) {
95              StatusLogger.getLogger().error(message, e);
96              return getFormat().getCommentMarker() + " " + e;
97          }
98      }
99  
100 }