1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender.db.jdbc;
18
19 import org.apache.logging.log4j.Logger;
20 import org.apache.logging.log4j.core.config.Configuration;
21 import org.apache.logging.log4j.core.config.plugins.Plugin;
22 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
23 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
24 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25 import org.apache.logging.log4j.core.layout.PatternLayout;
26 import org.apache.logging.log4j.core.util.Booleans;
27 import org.apache.logging.log4j.status.StatusLogger;
28 import org.apache.logging.log4j.util.Strings;
29
30
31
32
33 @Plugin(name = "Column", category = "Core", printObject = true)
34 public final class ColumnConfig {
35 private static final Logger LOGGER = StatusLogger.getLogger();
36
37 private final String columnName;
38 private final PatternLayout layout;
39 private final String literalValue;
40 private final boolean eventTimestamp;
41 private final boolean unicode;
42 private final boolean clob;
43
44 private ColumnConfig(final String columnName, final PatternLayout layout, final String literalValue,
45 final boolean eventDate, final boolean unicode, final boolean clob) {
46 this.columnName = columnName;
47 this.layout = layout;
48 this.literalValue = literalValue;
49 this.eventTimestamp = eventDate;
50 this.unicode = unicode;
51 this.clob = clob;
52 }
53
54 public String getColumnName() {
55 return this.columnName;
56 }
57
58 public PatternLayout getLayout() {
59 return this.layout;
60 }
61
62 public String getLiteralValue() {
63 return this.literalValue;
64 }
65
66 public boolean isEventTimestamp() {
67 return this.eventTimestamp;
68 }
69
70 public boolean isUnicode() {
71 return this.unicode;
72 }
73
74 public boolean isClob() {
75 return this.clob;
76 }
77
78 @Override
79 public String toString() {
80 return "{ name=" + this.columnName + ", layout=" + this.layout + ", literal=" + this.literalValue
81 + ", timestamp=" + this.eventTimestamp + " }";
82 }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 @PluginFactory
101 public static ColumnConfig createColumnConfig(
102 @PluginConfiguration final Configuration config,
103 @PluginAttribute("name") final String name,
104 @PluginAttribute("pattern") final String pattern,
105 @PluginAttribute("literal") final String literalValue,
106 @PluginAttribute("isEventTimestamp") final String eventTimestamp,
107 @PluginAttribute("isUnicode") final String unicode,
108 @PluginAttribute("isClob") final String clob) {
109 if (Strings.isEmpty(name)) {
110 LOGGER.error("The column config is not valid because it does not contain a column name.");
111 return null;
112 }
113
114 final boolean isPattern = Strings.isNotEmpty(pattern);
115 final boolean isLiteralValue = Strings.isNotEmpty(literalValue);
116 final boolean isEventTimestamp = Boolean.parseBoolean(eventTimestamp);
117 final boolean isUnicode = Booleans.parseBoolean(unicode, true);
118 final boolean isClob = Boolean.parseBoolean(clob);
119
120 if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) {
121 LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive.");
122 return null;
123 }
124
125 if (isEventTimestamp) {
126 return new ColumnConfig(name, null, null, true, false, false);
127 }
128 if (isLiteralValue) {
129 return new ColumnConfig(name, null, literalValue, false, false, false);
130 }
131 if (isPattern) {
132 final PatternLayout layout =
133 PatternLayout.newBuilder()
134 .withPattern(pattern)
135 .withConfiguration(config)
136 .withAlwaysWriteExceptions(false)
137 .build();
138 return new ColumnConfig(name, layout, null, false, isUnicode, isClob);
139 }
140
141 LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true.");
142 return null;
143 }
144 }