1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.status.StatusLogger;
32
33
34
35
36
37
38
39
40 @Plugin(name = "CsvLogEventLayout", category = Node.CATEGORY, elementType = Layout.ELEMENT_TYPE, printObject = true)
41 public class CsvLogEventLayout extends AbstractCsvLayout {
42
43 private static final long serialVersionUID = 1L;
44
45 public static CsvLogEventLayout createDefaultLayout() {
46 return new CsvLogEventLayout(Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
47 }
48
49 public static CsvLogEventLayout createLayout(final CSVFormat format) {
50 return new CsvLogEventLayout(Charset.forName(DEFAULT_CHARSET), format, null, null);
51 }
52
53 @PluginFactory
54 public static CsvLogEventLayout createLayout(
55
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
67 {
68
69 final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
70 return new CsvLogEventLayout(charset, csvFormat, header, footer);
71 }
72
73 protected CsvLogEventLayout(final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
74 super(charset, csvFormat, header, footer);
75 }
76
77 @Override
78 public String toSerializable(final LogEvent event) {
79 final StringBuilder buffer = getStringBuilder();
80
81
82
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.getThreadName());
88 printer.print(event.getMessage().getFormattedMessage());
89 printer.print(event.getLoggerFqcn());
90 printer.print(event.getLoggerName());
91 printer.print(event.getMarker());
92 printer.print(event.getThrownProxy());
93 printer.print(event.getSource());
94 printer.print(event.getContextMap());
95 printer.print(event.getContextStack());
96 printer.println();
97 return buffer.toString();
98 } catch (final IOException e) {
99 StatusLogger.getLogger().error(event.toString(), e);
100 return getFormat().getCommentMarker() + " " + e;
101 }
102 }
103
104 }