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.PluginAttribute;
    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.core.util.Booleans;
    27  import org.apache.logging.log4j.status.StatusLogger;
    28  import org.apache.logging.log4j.util.Strings;
    29
    30  /**
    31   * A configuration element used to configure which event properties are logged to which columns in the database table.
    32   */
    33  @Plugin(name = "Column", category = "Core", printObject = true)
    34  public final class ColumnConfig {
    35      private static final Logger LOGGER = StatusLogger.getLogger();
    36
    37      private final String columnName;
    38      private final PatternLayout layout;
    39      private final String literalValue;
    40      private final boolean eventTimestamp;
    41      private final boolean unicode;
    42      private final boolean clob;
    43
    44      private ColumnConfig(final String columnName, final PatternLayout layout, final String literalValue,
    45                           final boolean eventDate, final boolean unicode, final boolean clob) {
    46          this.columnName = columnName;
    47          this.layout = layout;
    48          this.literalValue = literalValue;
    49          this.eventTimestamp = eventDate;
    50          this.unicode = unicode;
    51          this.clob = clob;
    52      }
    53
    54      public String getColumnName() {
    55          return this.columnName;
    56      }
    57
    58      public PatternLayout getLayout() {
    59          return this.layout;
    60      }
    61
    62      public String getLiteralValue() {
    63          return this.literalValue;
    64      }
    65
    66      public boolean isEventTimestamp() {
    67          return this.eventTimestamp;
    68      }
    69
    70      public boolean isUnicode() {
    71          return this.unicode;
    72      }
    73
    74      public boolean isClob() {
    75          return this.clob;
    76      }
    77
    78      @Override
    79      public String toString() {
    80          return "{ name=" + this.columnName + ", layout=" + this.layout + ", literal=" + this.literalValue
    81                  + ", timestamp=" + this.eventTimestamp + " }";
    82      }
    83
    84      /**
    85       * Factory method for creating a column config within the plugin manager.
    86       *
    87       * @param config The configuration object
    88       * @param name The name of the database column as it exists within the database table.
    89       * @param pattern The {@link PatternLayout} pattern to insert in this column. Mutually exclusive with
    90       *                {@code literalValue!=null} and {@code eventTimestamp=true}
    91       * @param literalValue The literal value to insert into the column as-is without any quoting or escaping. Mutually
    92       *                     exclusive with {@code pattern!=null} and {@code eventTimestamp=true}.
    93       * @param eventTimestamp If {@code "true"}, indicates that this column is a date-time column in which the event
    94       *                       timestamp should be inserted. Mutually exclusive with {@code pattern!=null} and
    95       *                       {@code literalValue!=null}.
    96       * @param unicode If {@code "true"}, indicates that the column is a Unicode String.
    97       * @param clob If {@code "true"}, indicates that the column is a character LOB (CLOB).
    98       * @return the created column config.
    99       */
    100     @PluginFactory
    101     public static ColumnConfig createColumnConfig(
    102             @PluginConfiguration final Configuration config,
    103             @PluginAttribute("name") final String name,
    104             @PluginAttribute("pattern") final String pattern,
    105             @PluginAttribute("literal") final String literalValue,
    106             @PluginAttribute("isEventTimestamp") final String eventTimestamp,
    107             @PluginAttribute("isUnicode") final String unicode,
    108             @PluginAttribute("isClob") final String clob) {
    109         if (Strings.isEmpty(name)) {
    110             LOGGER.error("The column config is not valid because it does not contain a column name.");
    111             return null;
    112         }
    113
    114         final boolean isPattern = Strings.isNotEmpty(pattern);
    115         final boolean isLiteralValue = Strings.isNotEmpty(literalValue);
    116         final boolean isEventTimestamp = Boolean.parseBoolean(eventTimestamp);
    117         final boolean isUnicode = Booleans.parseBoolean(unicode, true);
    118         final boolean isClob = Boolean.parseBoolean(clob);
    119
    120         if ((isPattern && isLiteralValue) || (isPattern && isEventTimestamp) || (isLiteralValue && isEventTimestamp)) {
    121             LOGGER.error("The pattern, literal, and isEventTimestamp attributes are mutually exclusive.");
    122             return null;
    123         }
    124
    125         if (isEventTimestamp) {
    126             return new ColumnConfig(name, null, null, true, false, false);
    127         }
    128         if (isLiteralValue) {
    129             return new ColumnConfig(name, null, literalValue, false, false, false);
    130         }
    131         if (isPattern) {
    132             final PatternLayout layout =
    133                 PatternLayout.newBuilder()
    134                     .withPattern(pattern)
    135                     .withConfiguration(config)
    136                     .withAlwaysWriteExceptions(false)
    137                     .build();
    138             return new ColumnConfig(name, layout, null, false, isUnicode, isClob);
    139         }
    140
    141         LOGGER.error("To configure a column you must specify a pattern or literal or set isEventDate to true.");
    142         return null;
    143     }
    144 }