001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017 package org.apache.logging.log4j.core.appender.db.jdbc;
018
019 import org.apache.logging.log4j.core.Filter;
020 import org.apache.logging.log4j.core.appender.AbstractAppender;
021 import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
022 import org.apache.logging.log4j.core.config.plugins.Plugin;
023 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
024 import org.apache.logging.log4j.core.config.plugins.PluginElement;
025 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
026 import org.apache.logging.log4j.core.util.Booleans;
027
028 /**
029 * This Appender writes logging events to a relational database using standard JDBC mechanisms. It takes a list of
030 * {@link ColumnConfig}s with which it determines how to save the event data into the appropriate columns in the table.
031 * A {@link ConnectionSource} plugin instance instructs the appender (and {@link JdbcDatabaseManager}) how to connect to
032 * the database. This appender can be reconfigured at run time.
033 *
034 * @see ColumnConfig
035 * @see ConnectionSource
036 */
037 @Plugin(name = "JDBC", category = "Core", elementType = "appender", printObject = true)
038 public final class JdbcAppender extends AbstractDatabaseAppender<JdbcDatabaseManager> {
039 private static final long serialVersionUID = 1L;
040
041 private final String description;
042
043 private JdbcAppender(final String name, final Filter filter, final boolean ignoreExceptions,
044 final JdbcDatabaseManager manager) {
045 super(name, filter, ignoreExceptions, manager);
046 this.description = this.getName() + "{ manager=" + this.getManager() + " }";
047 }
048
049 @Override
050 public String toString() {
051 return this.description;
052 }
053
054 /**
055 * Factory method for creating a JDBC appender within the plugin manager.
056 *
057 * @param name The name of the appender.
058 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
059 * they are propagated to the caller.
060 * @param filter The filter, if any, to use.
061 * @param connectionSource The connections source from which database connections should be retrieved.
062 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
063 * the buffer reaches this size.
064 * @param tableName The name of the database table to insert log events into.
065 * @param columnConfigs Information about the columns that log event data should be inserted into and how to insert
066 * that data.
067 * @return a new JDBC appender.
068 */
069 @PluginFactory
070 public static JdbcAppender createAppender(
071 @PluginAttribute("name") final String name,
072 @PluginAttribute("ignoreExceptions") final String ignore,
073 @PluginElement("Filter") final Filter filter,
074 @PluginElement("ConnectionSource") final ConnectionSource connectionSource,
075 @PluginAttribute("bufferSize") final String bufferSize,
076 @PluginAttribute("tableName") final String tableName,
077 @PluginElement("ColumnConfigs") final ColumnConfig[] columnConfigs) {
078
079 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
080 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
081
082 final StringBuilder managerName = new StringBuilder("jdbcManager{ description=").append(name)
083 .append(", bufferSize=").append(bufferSizeInt).append(", connectionSource=")
084 .append(connectionSource.toString()).append(", tableName=").append(tableName).append(", columns=[ ");
085
086 int i = 0;
087 for (final ColumnConfig column : columnConfigs) {
088 if (i++ > 0) {
089 managerName.append(", ");
090 }
091 managerName.append(column.toString());
092 }
093
094 managerName.append(" ] }");
095
096 final JdbcDatabaseManager manager = JdbcDatabaseManager.getJDBCDatabaseManager(
097 managerName.toString(), bufferSizeInt, connectionSource, tableName, columnConfigs
098 );
099 if (manager == null) {
100 return null;
101 }
102
103 return new JdbcAppender(name, filter, ignoreExceptions, manager);
104 }
105 }