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.core.Filter;
    20  import org.apache.logging.log4j.core.appender.AbstractAppender;
    21  import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
    22  import org.apache.logging.log4j.core.config.plugins.Plugin;
    23  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
    24  import org.apache.logging.log4j.core.config.plugins.PluginElement;
    25  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
    26  import org.apache.logging.log4j.core.util.Booleans;
    27
    28  /**
    29   * This Appender writes logging events to a relational database using standard JDBC mechanisms. It takes a list of
    30   * {@link ColumnConfig}s with which it determines how to save the event data into the appropriate columns in the table.
    31   * A {@link ConnectionSource} plugin instance instructs the appender (and {@link JdbcDatabaseManager}) how to connect to
    32   * the database. This appender can be reconfigured at run time.
    33   *
    34   * @see ColumnConfig
    35   * @see ConnectionSource
    36   */
    37  @Plugin(name = "JDBC", category = "Core", elementType = "appender", printObject = true)
    38  public final class JdbcAppender extends AbstractDatabaseAppender<JdbcDatabaseManager> {
    39      private static final long serialVersionUID = 1L;
    40
    41      private final String description;
    42
    43      private JdbcAppender(final String name, final Filter filter, final boolean ignoreExceptions,
    44                           final JdbcDatabaseManager manager) {
    45          super(name, filter, ignoreExceptions, manager);
    46          this.description = this.getName() + "{ manager=" + this.getManager() + " }";
    47      }
    48
    49      @Override
    50      public String toString() {
    51          return this.description;
    52      }
    53
    54      /**
    55       * Factory method for creating a JDBC appender within the plugin manager.
    56       *
    57       * @param name The name of the appender.
    58       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
    59       *               they are propagated to the caller.
    60       * @param filter The filter, if any, to use.
    61       * @param connectionSource The connections source from which database connections should be retrieved.
    62       * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
    63       *                   the buffer reaches this size.
    64       * @param tableName The name of the database table to insert log events into.
    65       * @param columnConfigs Information about the columns that log event data should be inserted into and how to insert
    66       *                      that data.
    67       * @return a new JDBC appender.
    68       */
    69      @PluginFactory
    70      public static JdbcAppender createAppender(
    71              @PluginAttribute("name") final String name,
    72              @PluginAttribute("ignoreExceptions") final String ignore,
    73              @PluginElement("Filter") final Filter filter,
    74              @PluginElement("ConnectionSource") final ConnectionSource connectionSource,
    75              @PluginAttribute("bufferSize") final String bufferSize,
    76              @PluginAttribute("tableName") final String tableName,
    77              @PluginElement("ColumnConfigs") final ColumnConfig[] columnConfigs) {
    78
    79          final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
    80          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
    81
    82          final StringBuilder managerName = new StringBuilder("jdbcManager{ description=").append(name)
    83                  .append(", bufferSize=").append(bufferSizeInt).append(", connectionSource=")
    84                  .append(connectionSource.toString()).append(", tableName=").append(tableName).append(", columns=[ ");
    85
    86          int i = 0;
    87          for (final ColumnConfig column : columnConfigs) {
    88              if (i++ > 0) {
    89                  managerName.append(", ");
    90              }
    91              managerName.append(column.toString());
    92          }
    93
    94          managerName.append(" ] }");
    95
    96          final JdbcDatabaseManager manager = JdbcDatabaseManager.getJDBCDatabaseManager(
    97                  managerName.toString(), bufferSizeInt, connectionSource, tableName, columnConfigs
    98          );
    99          if (manager == null) {
    100             return null;
    101         }
    102
    103         return new JdbcAppender(name, filter, ignoreExceptions, manager);
    104     }
    105 }