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 public Builder setConfiguration(final Configuration configuration) {
152 this.configuration = configuration;
153 return this;
154 }
155
156
157
158
159 public Builder setName(final String name) {
160 this.name = name;
161 return this;
162 }
163
164
165
166
167
168 public Builder setPattern(final String pattern) {
169 this.pattern = pattern;
170 return this;
171 }
172
173
174
175
176
177 public Builder setLiteral(final String literal) {
178 this.literal = literal;
179 return this;
180 }
181
182
183
184
185
186 public Builder setEventTimestamp(final boolean eventTimestamp) {
187 isEventTimestamp = eventTimestamp;
188 return this;
189 }
190
191
192
193
194 public Builder setUnicode(final boolean unicode) {
195 isUnicode = unicode;
196 return this;
197 }
198
199
200
201
202 public Builder setClob(final boolean clob) {
203 isClob = clob;
204 return this;
205 }
206
207 @Override
208 public ColumnConfig build() {
209 if (Strings.isEmpty(name)) {
210 LOGGER.error("The column config is not valid because it does not contain a column name.");
211 return null;
212 }
213
214 final boolean isPattern = Strings.isNotEmpty(pattern);
215 final boolean isLiteralValue = Strings.isNotEmpty(literal);
216
217 if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) {
218 LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive.");
219 return null;
220 }
221
222 if (isEventTimestamp) {
223 return new ColumnConfig(name, null, null, true, false, false);
224 }
225
226 if (isLiteralValue) {
227 return new ColumnConfig(name, null, literal, false, false, false);
228 }
229
230 if (isPattern) {
231 final PatternLayout layout =
232 PatternLayout.newBuilder()
233 .withPattern(pattern)
234 .withConfiguration(configuration)
235 .withAlwaysWriteExceptions(false)
236 .build();
237 return new ColumnConfig(name, layout, null, false, isUnicode, isClob);
238 }
239
240 LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true.");
241 return null;
242 }
243 }
244 }