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