View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * A configuration element used to configure which event properties are logged to which columns in the database table.
35   *
36   * @see ColumnMapping
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       * Factory method for creating a column config within the plugin manager.
91       *
92       * @see Builder
93       * @deprecated use {@link #newBuilder()}
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          * The configuration object.
150          * 
151          * @return this. 
152          */
153         public Builder setConfiguration(final Configuration configuration) {
154             this.configuration = configuration;
155             return this;
156         }
157 
158         /**
159          * The name of the database column as it exists within the database table.
160          * 
161          * @return this. 
162          */
163         public Builder setName(final String name) {
164             this.name = name;
165             return this;
166         }
167 
168         /**
169          * The {@link PatternLayout} pattern to insert in this column. Mutually exclusive with
170          * {@code literal!=null} and {@code eventTimestamp=true}
171          * 
172          * @return this. 
173          */
174         public Builder setPattern(final String pattern) {
175             this.pattern = pattern;
176             return this;
177         }
178 
179         /**
180          * The literal value to insert into the column as-is without any quoting or escaping. Mutually exclusive with
181          * {@code pattern!=null} and {@code eventTimestamp=true}.
182          * 
183          * @return this. 
184          */
185         public Builder setLiteral(final String literal) {
186             this.literal = literal;
187             return this;
188         }
189 
190         /**
191          * If {@code "true"}, indicates that this column is a date-time column in which the event timestamp should be
192          * inserted. Mutually exclusive with {@code pattern!=null} and {@code literal!=null}.
193          * 
194          * @return this. 
195          */
196         public Builder setEventTimestamp(final boolean eventTimestamp) {
197             isEventTimestamp = eventTimestamp;
198             return this;
199         }
200 
201         /**
202          * If {@code "true"}, indicates that the column is a Unicode String.
203          * 
204          * @return this. 
205          */
206         public Builder setUnicode(final boolean unicode) {
207             isUnicode = unicode;
208             return this;
209         }
210 
211         /**
212          * If {@code "true"}, indicates that the column is a character LOB (CLOB).
213          * 
214          * @return this. 
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 }