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 */
017package org.apache.logging.log4j.core.appender.db.jdbc;
018
019import org.apache.logging.log4j.core.Filter;
020import org.apache.logging.log4j.core.appender.AbstractAppender;
021import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
024import org.apache.logging.log4j.core.config.plugins.PluginElement;
025import org.apache.logging.log4j.core.config.plugins.PluginFactory;
026import org.apache.logging.log4j.core.helpers.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)
038public final class JDBCAppender extends AbstractDatabaseAppender<JDBCDatabaseManager> {
039    private final String description;
040
041    private JDBCAppender(final String name, final Filter filter, final boolean ignoreExceptions,
042                         final JDBCDatabaseManager manager) {
043        super(name, filter, ignoreExceptions, manager);
044        this.description = this.getName() + "{ manager=" + this.getManager() + " }";
045    }
046
047    @Override
048    public String toString() {
049        return this.description;
050    }
051
052    /**
053     * Factory method for creating a JDBC appender within the plugin manager.
054     *
055     * @param name The name of the appender.
056     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
057     *               they are propagated to the caller.
058     * @param filter The filter, if any, to use.
059     * @param connectionSource The connections source from which database connections should be retrieved.
060     * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
061     *                   the buffer reaches this size.
062     * @param tableName The name of the database table to insert log events into.
063     * @param columnConfigs Information about the columns that log event data should be inserted into and how to insert
064     *                      that data.
065     * @return a new JDBC appender.
066     */
067    @PluginFactory
068    public static JDBCAppender createAppender(
069            @PluginAttribute("name") final String name,
070            @PluginAttribute("ignoreExceptions") final String ignore,
071            @PluginElement("Filter") final Filter filter,
072            @PluginElement("ConnectionSource") final ConnectionSource connectionSource,
073            @PluginAttribute("bufferSize") final String bufferSize,
074            @PluginAttribute("tableName") final String tableName,
075            @PluginElement("ColumnConfigs") final ColumnConfig[] columnConfigs) {
076
077        final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
078        final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
079
080        final StringBuilder managerName = new StringBuilder("jdbcManager{ description=").append(name)
081                .append(", bufferSize=").append(bufferSizeInt).append(", connectionSource=")
082                .append(connectionSource.toString()).append(", tableName=").append(tableName).append(", columns=[ ");
083
084        int i = 0;
085        for (final ColumnConfig column : columnConfigs) {
086            if (i++ > 0) {
087                managerName.append(", ");
088            }
089            managerName.append(column.toString());
090        }
091
092        managerName.append(" ] }");
093
094        final JDBCDatabaseManager manager = JDBCDatabaseManager.getJDBCDatabaseManager(
095                managerName.toString(), bufferSizeInt, connectionSource, tableName, columnConfigs
096        );
097        if (manager == null) {
098            return null;
099        }
100
101        return new JDBCAppender(name, filter, ignoreExceptions, manager);
102    }
103}