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.PluginAttr;
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.status.StatusLogger;
27
28
29
30
31 @Plugin(name = "Column", category = "Core", printObject = true)
32 public final class ColumnConfig {
33 private static final Logger LOGGER = StatusLogger.getLogger();
34
35 private final String columnName;
36 private final PatternLayout layout;
37 private final String literalValue;
38 private final boolean eventTimestamp;
39 private final boolean unicode;
40 private final boolean clob;
41
42 private ColumnConfig(final String columnName, final PatternLayout layout, final String literalValue,
43 final boolean eventDate, final boolean unicode, final boolean clob) {
44 this.columnName = columnName;
45 this.layout = layout;
46 this.literalValue = literalValue;
47 this.eventTimestamp = eventDate;
48 this.unicode = unicode;
49 this.clob = clob;
50 }
51
52 public String getColumnName() {
53 return this.columnName;
54 }
55
56 public PatternLayout getLayout() {
57 return this.layout;
58 }
59
60 public String getLiteralValue() {
61 return this.literalValue;
62 }
63
64 public boolean isEventTimestamp() {
65 return this.eventTimestamp;
66 }
67
68 public boolean isUnicode() {
69 return this.unicode;
70 }
71
72 public boolean isClob() {
73 return this.clob;
74 }
75
76 @Override
77 public String toString() {
78 return "{ name=" + this.columnName + ", layout=" + this.layout + ", literal=" + this.literalValue
79 + ", timestamp=" + this.eventTimestamp + " }";
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96 @PluginFactory
97 public static ColumnConfig createColumnConfig(@PluginConfiguration final Configuration config,
98 @PluginAttr("name") final String name,
99 @PluginAttr("pattern") final String pattern,
100 @PluginAttr("literal") final String literalValue,
101 @PluginAttr("isEventTimestamp") final String eventTimestamp,
102 @PluginAttr("isUnicode") final String unicode,
103 @PluginAttr("isClob") final String clob) {
104 if (name == null || name.length() == 0) {
105 LOGGER.error("The column config is not valid because it does not contain a column name.");
106 return null;
107 }
108
109 final boolean isPattern = pattern != null && pattern.length() > 0;
110 final boolean isLiteralValue = literalValue != null && literalValue.length() > 0;
111 final boolean isEventTimestamp = eventTimestamp != null && Boolean.parseBoolean(eventTimestamp);
112 final boolean isUnicode = unicode == null || unicode.length() == 0 || Boolean.parseBoolean(unicode);
113 final boolean isClob = clob != null && Boolean.parseBoolean(clob);
114
115 if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) {
116 LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive.");
117 return null;
118 }
119
120 if (isEventTimestamp) {
121 return new ColumnConfig(name, null, null, true, false, false);
122 }
123 if (isLiteralValue) {
124 return new ColumnConfig(name, null, literalValue, false, false, false);
125 }
126 if (isPattern) {
127 return new ColumnConfig(
128 name, PatternLayout.createLayout(pattern, config, null, null, "true"), null, false, isUnicode,
129 isClob
130 );
131 }
132
133 LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true.");
134 return null;
135 }
136 }