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