1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache license, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the license for the specific language governing permissions and
15 * limitations under the license.
16 */
17 package org.apache.logging.log4j.nosql.appender;
18
19 import org.apache.logging.log4j.core.Filter;
20 import org.apache.logging.log4j.core.appender.AbstractAppender;
21 import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
22 import org.apache.logging.log4j.core.config.plugins.Plugin;
23 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
24 import org.apache.logging.log4j.core.config.plugins.PluginElement;
25 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
26 import org.apache.logging.log4j.core.util.Booleans;
27
28 /**
29 * This Appender writes logging events to a NoSQL database using a configured NoSQL provider. It requires
30 * implementations of {@link NoSqlObject}, {@link NoSqlConnection}, and {@link NoSqlProvider} to "know" how to write
31 * events to the chosen NoSQL database. Two provider implementations are provided: MongoDB
32 * (org.mongodb:mongo-java-driver:2.11.1 or newer must be on the classpath) and Apache CouchDB
33 * (org.lightcouch:lightcouch:0.0.5 or newer must be on the classpath). For examples on how to write your own NoSQL
34 * provider, see the simple source code for the MongoDB and CouchDB providers.
35 *
36 * @see NoSqlObject
37 * @see NoSqlConnection
38 * @see NoSqlProvider
39 */
40 @Plugin(name = "NoSql", category = "Core", elementType = "appender", printObject = true)
41 public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseManager<?>> {
42 private static final long serialVersionUID = 1L;
43 private final String description;
44
45 private NoSqlAppender(final String name, final Filter filter, final boolean ignoreExceptions,
46 final NoSqlDatabaseManager<?> manager) {
47 super(name, filter, ignoreExceptions, manager);
48 this.description = this.getName() + "{ manager=" + this.getManager() + " }";
49 }
50
51 @Override
52 public String toString() {
53 return this.description;
54 }
55
56 /**
57 * Factory method for creating a NoSQL appender within the plugin manager.
58 *
59 * @param name The name of the appender.
60 * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
61 * they are propagated to the caller.
62 * @param filter The filter, if any, to use.
63 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
64 * the buffer reaches this size.
65 * @param provider The NoSQL provider that provides connections to the chosen NoSQL database.
66 * @return a new NoSQL appender.
67 */
68 @PluginFactory
69 public static NoSqlAppender createAppender(
70 @PluginAttribute("name") final String name,
71 @PluginAttribute("ignoreExceptions") final String ignore,
72 @PluginElement("Filter") final Filter filter,
73 @PluginAttribute("bufferSize") final String bufferSize,
74 @PluginElement("NoSqlProvider") final NoSqlProvider<?> provider) {
75 if (provider == null) {
76 LOGGER.error("NoSQL provider not specified for appender [{}].", name);
77 return null;
78 }
79
80 final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
81 final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
82
83 final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt
84 + ", provider=" + provider + " }";
85
86 final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(
87 managerName, bufferSizeInt, provider
88 );
89 if (manager == null) {
90 return null;
91 }
92
93 return new NoSqlAppender(name, filter, ignoreExceptions, manager);
94 }
95 }