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