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.Core;
21 import org.apache.logging.log4j.core.appender.db.ColumnMapping;
22 import org.apache.logging.log4j.core.config.Configuration;
23 import org.apache.logging.log4j.core.config.plugins.Plugin;
24 import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
25 import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
26 import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
27 import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
28 import org.apache.logging.log4j.core.layout.PatternLayout;
29 import org.apache.logging.log4j.core.util.Booleans;
30 import org.apache.logging.log4j.status.StatusLogger;
31 import org.apache.logging.log4j.util.Strings;
32
33
34
35
36
37
38 @Plugin(name = "Column", category = Core.CATEGORY_NAME, printObject = true)
39 public final class ColumnConfig {
40 private static final Logger LOGGER = StatusLogger.getLogger();
41
42 private final String columnName;
43 private final PatternLayout layout;
44 private final String literalValue;
45 private final boolean eventTimestamp;
46 private final boolean unicode;
47 private final boolean clob;
48
49 private ColumnConfig(final String columnName, final PatternLayout layout, final String literalValue,
50 final boolean eventDate, final boolean unicode, final boolean clob) {
51 this.columnName = columnName;
52 this.layout = layout;
53 this.literalValue = literalValue;
54 this.eventTimestamp = eventDate;
55 this.unicode = unicode;
56 this.clob = clob;
57 }
58
59 public String getColumnName() {
60 return this.columnName;
61 }
62
63 public PatternLayout getLayout() {
64 return this.layout;
65 }
66
67 public String getLiteralValue() {
68 return this.literalValue;
69 }
70
71 public boolean isEventTimestamp() {
72 return this.eventTimestamp;
73 }
74
75 public boolean isUnicode() {
76 return this.unicode;
77 }
78
79 public boolean isClob() {
80 return this.clob;
81 }
82
83 @Override
84 public String toString() {
85 return "{ name=" + this.columnName + ", layout=" + this.layout + ", literal=" + this.literalValue
86 + ", timestamp=" + this.eventTimestamp + " }";
87 }
88
89
90
91
92
93
94
95 @Deprecated
96 public static ColumnConfig createColumnConfig(final Configuration config, final String name, final String pattern,
97 final String literalValue, final String eventTimestamp,
98 final String unicode, final String clob) {
99 if (Strings.isEmpty(name)) {
100 LOGGER.error("The column config is not valid because it does not contain a column name.");
101 return null;
102 }
103
104 final boolean isEventTimestamp = Boolean.parseBoolean(eventTimestamp);
105 final boolean isUnicode = Booleans.parseBoolean(unicode, true);
106 final boolean isClob = Boolean.parseBoolean(clob);
107
108 return newBuilder()
109 .setConfiguration(config)
110 .setName(name)
111 .setPattern(pattern)
112 .setLiteral(literalValue)
113 .setEventTimestamp(isEventTimestamp)
114 .setUnicode(isUnicode)
115 .setClob(isClob)
116 .build();
117 }
118
119 @PluginBuilderFactory
120 public static Builder newBuilder() {
121 return new Builder();
122 }
123
124 public static class Builder implements org.apache.logging.log4j.core.util.Builder<ColumnConfig> {
125
126 @PluginConfiguration
127 private Configuration configuration;
128
129 @PluginBuilderAttribute
130 @Required(message = "No name provided")
131 private String name;
132
133 @PluginBuilderAttribute
134 private String pattern;
135
136 @PluginBuilderAttribute
137 private String literal;
138
139 @PluginBuilderAttribute
140 private boolean isEventTimestamp;
141
142 @PluginBuilderAttribute
143 private boolean isUnicode = true;
144
145 @PluginBuilderAttribute
146 private boolean isClob;
147
148
149
150
151
152
153 public Builder setConfiguration(final Configuration configuration) {
154 this.configuration = configuration;
155 return this;
156 }
157
158
159
160
161
162
163 public Builder setName(final String name) {
164 this.name = name;
165 return this;
166 }
167
168
169
170
171
172
173
174 public Builder setPattern(final String pattern) {
175 this.pattern = pattern;
176 return this;
177 }
178
179
180
181
182
183
184
185 public Builder setLiteral(final String literal) {
186 this.literal = literal;
187 return this;
188 }
189
190
191
192
193
194
195
196 public Builder setEventTimestamp(final boolean eventTimestamp) {
197 isEventTimestamp = eventTimestamp;
198 return this;
199 }
200
201
202
203
204
205
206 public Builder setUnicode(final boolean unicode) {
207 isUnicode = unicode;
208 return this;
209 }
210
211
212
213
214
215
216 public Builder setClob(final boolean clob) {
217 isClob = clob;
218 return this;
219 }
220
221 @Override
222 public ColumnConfig build() {
223 if (Strings.isEmpty(name)) {
224 LOGGER.error("The column config is not valid because it does not contain a column name.");
225 return null;
226 }
227
228 final boolean isPattern = Strings.isNotEmpty(pattern);
229 final boolean isLiteralValue = Strings.isNotEmpty(literal);
230
231 if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) {
232 LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive.");
233 return null;
234 }
235
236 if (isEventTimestamp) {
237 return new ColumnConfig(name, null, null, true, false, false);
238 }
239
240 if (isLiteralValue) {
241 return new ColumnConfig(name, null, literal, false, false, false);
242 }
243
244 if (isPattern) {
245 final PatternLayout layout =
246 PatternLayout.newBuilder()
247 .withPattern(pattern)
248 .withConfiguration(configuration)
249 .withAlwaysWriteExceptions(false)
250 .build();
251 return new ColumnConfig(name, layout, null, false, isUnicode, isClob);
252 }
253
254 LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true.");
255 return null;
256 }
257 }
258 }