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 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    }