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.helpers.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 final String description;
40  
41      private JDBCAppender(final String name, final Filter filter, final boolean ignoreExceptions,
42                           final JDBCDatabaseManager manager) {
43          super(name, filter, ignoreExceptions, manager);
44          this.description = this.getName() + "{ manager=" + this.getManager() + " }";
45      }
46  
47      @Override
48      public String toString() {
49          return this.description;
50      }
51  
52      /**
53       * Factory method for creating a JDBC appender within the plugin manager.
54       *
55       * @param name The name of the appender.
56       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
57       *               they are propagated to the caller.
58       * @param filter The filter, if any, to use.
59       * @param connectionSource The connections source from which database connections should be retrieved.
60       * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
61       *                   the buffer reaches this size.
62       * @param tableName The name of the database table to insert log events into.
63       * @param columnConfigs Information about the columns that log event data should be inserted into and how to insert
64       *                      that data.
65       * @return a new JDBC appender.
66       */
67      @PluginFactory
68      public static JDBCAppender createAppender(
69              @PluginAttribute("name") final String name,
70              @PluginAttribute("ignoreExceptions") final String ignore,
71              @PluginElement("Filter") final Filter filter,
72              @PluginElement("ConnectionSource") final ConnectionSource connectionSource,
73              @PluginAttribute("bufferSize") final String bufferSize,
74              @PluginAttribute("tableName") final String tableName,
75              @PluginElement("ColumnConfigs") final ColumnConfig[] columnConfigs) {
76  
77          final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
78          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
79  
80          final StringBuilder managerName = new StringBuilder("jdbcManager{ description=").append(name)
81                  .append(", bufferSize=").append(bufferSizeInt).append(", connectionSource=")
82                  .append(connectionSource.toString()).append(", tableName=").append(tableName).append(", columns=[ ");
83  
84          int i = 0;
85          for (final ColumnConfig column : columnConfigs) {
86              if (i++ > 0) {
87                  managerName.append(", ");
88              }
89              managerName.append(column.toString());
90          }
91  
92          managerName.append(" ] }");
93  
94          final JDBCDatabaseManager manager = JDBCDatabaseManager.getJDBCDatabaseManager(
95                  managerName.toString(), bufferSizeInt, connectionSource, tableName, columnConfigs
96          );
97          if (manager == null) {
98              return null;
99          }
100 
101         return new JDBCAppender(name, filter, ignoreExceptions, manager);
102     }
103 }