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.nosql;
018
019import org.apache.logging.log4j.core.Appender;
020import org.apache.logging.log4j.core.Filter;
021import org.apache.logging.log4j.core.appender.AbstractAppender;
022import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
025import org.apache.logging.log4j.core.config.plugins.PluginElement;
026import org.apache.logging.log4j.core.config.plugins.PluginFactory;
027import org.apache.logging.log4j.core.util.Booleans;
028
029/**
030 * This Appender writes logging events to a NoSQL database using a configured NoSQL provider. It requires
031 * implementations of {@link NoSqlObject}, {@link NoSqlConnection}, and {@link NoSqlProvider} to "know" how to write
032 * events to the chosen NoSQL database.
033 * 
034 * For examples on how to write your own NoSQL provider, see the simple source code for the MongoDB and CouchDB
035 * providers.
036 * </p>
037 * 
038 * @see NoSqlObject
039 * @see NoSqlConnection
040 * @see NoSqlProvider
041 */
042@Plugin(name = "NoSql", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true)
043public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseManager<?>> {
044    private final String description;
045
046    private NoSqlAppender(final String name, final Filter filter, final boolean ignoreExceptions,
047                          final NoSqlDatabaseManager<?> manager) {
048        super(name, filter, ignoreExceptions, manager);
049        this.description = this.getName() + "{ manager=" + this.getManager() + " }";
050    }
051
052    @Override
053    public String toString() {
054        return this.description;
055    }
056
057    /**
058     * Factory method for creating a NoSQL appender within the plugin manager.
059     *
060     * @param name The name of the appender.
061     * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
062     *               they are propagated to the caller.
063     * @param filter The filter, if any, to use.
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 provider The NoSQL provider that provides connections to the chosen NoSQL database.
067     * @return a new NoSQL appender.
068     */
069    @PluginFactory
070    public static NoSqlAppender createAppender(
071            @PluginAttribute("name") final String name,
072            @PluginAttribute("ignoreExceptions") final String ignore,
073            @PluginElement("Filter") final Filter filter,
074            @PluginAttribute("bufferSize") final String bufferSize,
075            @PluginElement("NoSqlProvider") final NoSqlProvider<?> provider) {
076        if (provider == null) {
077            LOGGER.error("NoSQL provider not specified for appender [{}].", name);
078            return null;
079        }
080
081        final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
082        final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
083
084        final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt
085                + ", provider=" + provider + " }";
086
087        final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(
088                managerName, bufferSizeInt, provider
089        );
090        if (manager == null) {
091            return null;
092        }
093
094        return new NoSqlAppender(name, filter, ignoreExceptions, manager);
095    }
096}