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.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.PluginFactory;
31  import org.apache.logging.log4j.message.Message;
32  import org.apache.logging.log4j.status.StatusLogger;
33  
34  /**
35   * A Comma-Separated Value (CSV) layout to log event parameters.
36   * The event message is currently ignored. 
37   * 
38   * <p>
39   * Best used with:
40   * </p>
41   * <p>
42   * {@code logger.debug(new ObjectArrayMessage(1, 2, "Bob"));}
43   * </p>
44   * 
45   * Depends on Apache Commons CSV 1.2.
46   * 
47   * @since 2.4
48   */
49  @Plugin(name = "CsvParameterLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
50  public class CsvParameterLayout extends AbstractCsvLayout {
51  
52      private static final long serialVersionUID = 1L;
53  
54      public static AbstractCsvLayout createDefaultLayout() {
55          return new CsvParameterLayout(Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
56      }
57  
58      public static AbstractCsvLayout createLayout(final CSVFormat format) {
59          return new CsvParameterLayout(Charset.forName(DEFAULT_CHARSET), format, null, null);
60      }
61  
62      @PluginFactory
63      public static AbstractCsvLayout createLayout(
64              // @formatter:off
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(charset, csvFormat, header, footer);
80      }
81  
82      public CsvParameterLayout(final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
83          super(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 = new StringBuilder(1024);
91          try {
92              // Revisit when 1.3 is out so that we do not need to create a new
93              // printer for each event.
94              // No need to close the printer.
95              final CSVPrinter printer = new CSVPrinter(buffer, getFormat());
96              printer.printRecord(parameters);
97              return buffer.toString();
98          } catch (final IOException e) {
99              StatusLogger.getLogger().error(message, e);
100             return getFormat().getCommentMarker() + " " + e;
101         }
102     }
103 
104 }