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.status.StatusLogger;
34  
35  /**
36   * A Comma-Separated Value (CSV) layout to log events.
37   * 
38   * Depends on Apache Commons CSV 1.2.
39   * 
40   * @since 2.4
41   */
42  @Plugin(name = "CsvLogEventLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
43  public class CsvLogEventLayout extends AbstractCsvLayout {
44  
45      public static CsvLogEventLayout createDefaultLayout() {
46          return new CsvLogEventLayout(null, Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
47      }
48  
49      public static CsvLogEventLayout createLayout(final CSVFormat format) {
50          return new CsvLogEventLayout(null, Charset.forName(DEFAULT_CHARSET), format, null, null);
51      }
52  
53      @PluginFactory
54      public static CsvLogEventLayout createLayout(
55              // @formatter:off
56              @PluginConfiguration final Configuration config,
57              @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format,
58              @PluginAttribute("delimiter") final Character delimiter,
59              @PluginAttribute("escape") final Character escape,
60              @PluginAttribute("quote") final Character quote,
61              @PluginAttribute("quoteMode") final QuoteMode quoteMode,
62              @PluginAttribute("nullString") final String nullString,
63              @PluginAttribute("recordSeparator") final String recordSeparator,
64              @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset,
65              @PluginAttribute("header") final String header, 
66              @PluginAttribute("footer") final String footer)
67              // @formatter:on
68      {
69  
70          final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
71          return new CsvLogEventLayout(config, charset, csvFormat, header, footer);
72      }
73     
74      protected CsvLogEventLayout(final Configuration config, final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
75          super(config, charset, csvFormat, header, footer);
76      }
77  
78      @Override
79      public String toSerializable(final LogEvent event) {
80          final StringBuilder buffer = getStringBuilder();
81          // Revisit when 1.3 is out so that we do not need to create a new
82          // printer for each event.
83          try (final CSVPrinter printer = new CSVPrinter(buffer, getFormat())) {
84              printer.print(event.getNanoTime());
85              printer.print(event.getTimeMillis());
86              printer.print(event.getLevel());
87              printer.print(event.getThreadId());
88              printer.print(event.getThreadName());
89              printer.print(event.getThreadPriority());
90              printer.print(event.getMessage().getFormattedMessage());
91              printer.print(event.getLoggerFqcn());
92              printer.print(event.getLoggerName());
93              printer.print(event.getMarker());
94              printer.print(event.getThrownProxy());
95              printer.print(event.getSource());
96              printer.print(event.getContextMap());
97              printer.print(event.getContextStack());
98              printer.println();
99              return buffer.toString();
100         } catch (final IOException e) {
101             StatusLogger.getLogger().error(event.toString(), e);
102             return getFormat().getCommentMarker() + " " + e;
103         }
104     }
105 
106 }