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