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
44
45
46 private static final long serialVersionUID = 1L;
47
48 public static CsvLogEventLayout createDefaultLayout() {
49 return new CsvLogEventLayout(Charset.forName(DEFAULT_CHARSET), CSVFormat.valueOf(DEFAULT_FORMAT), null, null);
50 }
51
52 public static CsvLogEventLayout createLayout(final CSVFormat format) {
53 return new CsvLogEventLayout(Charset.forName(DEFAULT_CHARSET), format, null, null);
54 }
55
56 @PluginFactory
57 public static CsvLogEventLayout createLayout(
58
59 @PluginAttribute(value = "format", defaultString = DEFAULT_FORMAT) final String format,
60 @PluginAttribute("delimiter") final Character delimiter,
61 @PluginAttribute("escape") final Character escape,
62 @PluginAttribute("quote") final Character quote,
63 @PluginAttribute("quoteMode") final QuoteMode quoteMode,
64 @PluginAttribute("nullString") final String nullString,
65 @PluginAttribute("recordSeparator") final String recordSeparator,
66 @PluginAttribute(value = "charset", defaultString = DEFAULT_CHARSET) final Charset charset,
67 @PluginAttribute("header") final String header,
68 @PluginAttribute("footer") final String footer)
69
70 {
71
72 final CSVFormat csvFormat = createFormat(format, delimiter, escape, quote, quoteMode, nullString, recordSeparator);
73 return new CsvLogEventLayout(charset, csvFormat, header, footer);
74 }
75
76 protected CsvLogEventLayout(final Charset charset, final CSVFormat csvFormat, final String header, final String footer) {
77 super(charset, csvFormat, header, footer);
78 }
79
80 @Override
81 public String toSerializable(final LogEvent event) {
82 final StringBuilder buffer = new StringBuilder(1024);
83 try {
84
85
86
87 final CSVPrinter printer = new CSVPrinter(buffer, getFormat());
88 printer.print(event.getNanoTime());
89 printer.print(event.getTimeMillis());
90 printer.print(event.getLevel());
91 printer.print(event.getThreadName());
92 printer.print(event.getMessage().getFormattedMessage());
93 printer.print(event.getLoggerFqcn());
94 printer.print(event.getLoggerName());
95 printer.print(event.getMarker());
96 printer.print(event.getThrownProxy());
97 printer.print(event.getSource());
98 printer.print(event.getContextMap());
99 printer.print(event.getContextStack());
100 printer.println();
101 return buffer.toString();
102 } catch (final IOException e) {
103 StatusLogger.getLogger().error(event.toString(), e);
104 return getFormat().getCommentMarker() + " " + e;
105 }
106 }
107
108 }