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 java.util.Objects; 020 021import org.apache.logging.log4j.core.Filter; 022import org.apache.logging.log4j.core.appender.AbstractAppender; 023import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender; 024import org.apache.logging.log4j.core.config.plugins.Plugin; 025import org.apache.logging.log4j.core.config.plugins.PluginAttribute; 026import org.apache.logging.log4j.core.config.plugins.PluginElement; 027import org.apache.logging.log4j.core.config.plugins.PluginFactory; 028import org.apache.logging.log4j.core.util.Booleans; 029 030/** 031 * This Appender writes logging events to a relational database using standard JDBC mechanisms. It takes a list of 032 * {@link ColumnConfig}s with which it determines how to save the event data into the appropriate columns in the table. 033 * A {@link ConnectionSource} plugin instance instructs the appender (and {@link JdbcDatabaseManager}) how to connect to 034 * the database. This appender can be reconfigured at run time. 035 * 036 * @see ColumnConfig 037 * @see ConnectionSource 038 */ 039@Plugin(name = "JDBC", category = "Core", elementType = "appender", printObject = true) 040public final class JdbcAppender extends AbstractDatabaseAppender<JdbcDatabaseManager> { 041 private static final long serialVersionUID = 1L; 042 043 private final String description; 044 045 private JdbcAppender(final String name, final Filter filter, final boolean ignoreExceptions, 046 final JdbcDatabaseManager manager) { 047 super(name, filter, ignoreExceptions, manager); 048 this.description = this.getName() + "{ manager=" + this.getManager() + " }"; 049 } 050 051 @Override 052 public String toString() { 053 return this.description; 054 } 055 056 /** 057 * Factory method for creating a JDBC appender within the plugin manager. 058 * 059 * @param name The name of the appender. 060 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 061 * they are propagated to the caller. 062 * @param filter The filter, if any, to use. 063 * @param connectionSource The connections source from which database connections should be retrieved. 064 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever 065 * the buffer reaches this size. 066 * @param tableName The name of the database table to insert log events into. 067 * @param columnConfigs Information about the columns that log event data should be inserted into and how to insert 068 * that data. 069 * @return a new JDBC appender. 070 */ 071 @PluginFactory 072 public static JdbcAppender createAppender( 073 @PluginAttribute("name") final String name, 074 @PluginAttribute("ignoreExceptions") final String ignore, 075 @PluginElement("Filter") final Filter filter, 076 @PluginElement("ConnectionSource") final ConnectionSource connectionSource, 077 @PluginAttribute("bufferSize") final String bufferSize, 078 @PluginAttribute("tableName") final String tableName, 079 @PluginElement("ColumnConfigs") final ColumnConfig[] columnConfigs) { 080 if (connectionSource == null) { 081 throw new NullPointerException("connectionSource"); 082 } 083 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); 084 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 085 086 final StringBuilder managerName = new StringBuilder("jdbcManager{ description=").append(name) 087 .append(", bufferSize=").append(bufferSizeInt).append(", connectionSource=") 088 .append(connectionSource.toString()).append(", tableName=").append(tableName).append(", columns=[ "); 089 090 int i = 0; 091 for (final ColumnConfig column : columnConfigs) { 092 if (i++ > 0) { 093 managerName.append(", "); 094 } 095 managerName.append(column.toString()); 096 } 097 098 managerName.append(" ] }"); 099 100 final JdbcDatabaseManager manager = JdbcDatabaseManager.getJDBCDatabaseManager( 101 managerName.toString(), bufferSizeInt, connectionSource, tableName, columnConfigs 102 ); 103 if (manager == null) { 104 return null; 105 } 106 107 return new JdbcAppender(name, filter, ignoreExceptions, manager); 108 } 109}