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.nosql.appender; 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 NoSQL database using a configured NoSQL provider. It requires 030 * implementations of {@link NoSqlObject}, {@link NoSqlConnection}, and {@link NoSqlProvider} to "know" how to write 031 * events to the chosen NoSQL database. 032 * 033 * <p> 034 * Two provider implementations are provided: 035 * </p> 036 * <ul> 037 * <li> 038 * MongoDB (org.mongodb:mongo-java-driver:2.11.1 or newer must be on the classpath)</li> 039 * <li> 040 * Apache CouchDB (org.lightcouch:lightcouch:0.0.5 or newer must be on the classpath).</li> 041 * </ul> 042 * <p> 043 * For examples on how to write your own NoSQL provider, see the simple source code for the MongoDB and CouchDB 044 * providers. 045 * </p> 046 * 047 * @see NoSqlObject 048 * @see NoSqlConnection 049 * @see NoSqlProvider 050 */ 051@Plugin(name = "NoSql", category = "Core", elementType = "appender", printObject = true) 052public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseManager<?>> { 053 private final String description; 054 055 private NoSqlAppender(final String name, final Filter filter, final boolean ignoreExceptions, 056 final NoSqlDatabaseManager<?> manager) { 057 super(name, filter, ignoreExceptions, manager); 058 this.description = this.getName() + "{ manager=" + this.getManager() + " }"; 059 } 060 061 @Override 062 public String toString() { 063 return this.description; 064 } 065 066 /** 067 * Factory method for creating a NoSQL appender within the plugin manager. 068 * 069 * @param name The name of the appender. 070 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise 071 * they are propagated to the caller. 072 * @param filter The filter, if any, to use. 073 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever 074 * the buffer reaches this size. 075 * @param provider The NoSQL provider that provides connections to the chosen NoSQL database. 076 * @return a new NoSQL appender. 077 */ 078 @PluginFactory 079 public static NoSqlAppender createAppender( 080 @PluginAttribute("name") final String name, 081 @PluginAttribute("ignoreExceptions") final String ignore, 082 @PluginElement("Filter") final Filter filter, 083 @PluginAttribute("bufferSize") final String bufferSize, 084 @PluginElement("NoSqlProvider") final NoSqlProvider<?> provider) { 085 if (provider == null) { 086 LOGGER.error("NoSQL provider not specified for appender [{}].", name); 087 return null; 088 } 089 090 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0); 091 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true); 092 093 final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt 094 + ", provider=" + provider + " }"; 095 096 final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager( 097 managerName, bufferSizeInt, provider 098 ); 099 if (manager == null) { 100 return null; 101 } 102 103 return new NoSqlAppender(name, filter, ignoreExceptions, manager); 104 } 105}