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