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.nosql.appender;
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 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. Two provider implementations are provided: MongoDB
032 * (org.mongodb:mongo-java-driver:2.11.1 or newer must be on the classpath) and Apache CouchDB
033 * (org.lightcouch:lightcouch:0.0.5 or newer must be on the classpath). For examples on how to write your own NoSQL
034 * provider, see the simple source code for the MongoDB and CouchDB providers.
035 *
036 * @see NoSqlObject
037 * @see NoSqlConnection
038 * @see NoSqlProvider
039 */
040 @Plugin(name = "NoSql", category = "Core", elementType = "appender", printObject = true)
041 public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseManager<?>> {
042 private static final long serialVersionUID = 1L;
043 private final String description;
044
045 private NoSqlAppender(final String name, final Filter filter, final boolean ignoreExceptions,
046 final NoSqlDatabaseManager<?> 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 NoSQL 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 bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
064 * the buffer reaches this size.
065 * @param provider The NoSQL provider that provides connections to the chosen NoSQL database.
066 * @return a new NoSQL appender.
067 */
068 @PluginFactory
069 public static NoSqlAppender createAppender(
070 @PluginAttribute("name") final String name,
071 @PluginAttribute("ignoreExceptions") final String ignore,
072 @PluginElement("Filter") final Filter filter,
073 @PluginAttribute("bufferSize") final String bufferSize,
074 @PluginElement("NoSqlProvider") final NoSqlProvider<?> provider) {
075 if (provider == null) {
076 LOGGER.error("NoSQL provider not specified for appender [{}].", name);
077 return null;
078 }
079
080 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
081 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
082
083 final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt
084 + ", provider=" + provider + " }";
085
086 final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(
087 managerName, bufferSizeInt, provider
088 );
089 if (manager == null) {
090 return null;
091 }
092
093 return new NoSqlAppender(name, filter, ignoreExceptions, manager);
094 }
095 }