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.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   * A configuration element used to configure which event properties are logged to which columns in the database table.
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       * Factory method for creating a column config within the plugin manager.
84       *
85       * @param config The configuration object
86       * @param name The name of the database column as it exists within the database table.
87       * @param pattern The {@link PatternLayout} pattern to insert in this column. Mutually exclusive with
88       *                {@code literalValue!=null} and {@code eventTimestamp=true}
89       * @param literalValue The literal value to insert into the column as-is without any quoting or escaping. Mutually
90       *                     exclusive with {@code pattern!=null} and {@code eventTimestamp=true}.
91       * @param eventTimestamp If {@code "true"}, indicates that this column is a date-time column in which the event
92       *                       timestamp should be inserted. Mutually exclusive with {@code pattern!=null} and
93       *                       {@code literalValue!=null}.
94       * @return the created column config.
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 }