View Javadoc

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.core.appender.db.nosql;
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.helpers.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 final String description;
43  
44      private NoSQLAppender(final String name, final Filter filter, final boolean ignoreExceptions,
45                            final NoSQLDatabaseManager<?> manager) {
46          super(name, filter, ignoreExceptions, manager);
47          this.description = this.getName() + "{ manager=" + this.getManager() + " }";
48      }
49  
50      @Override
51      public String toString() {
52          return this.description;
53      }
54  
55      /**
56       * Factory method for creating a NoSQL appender within the plugin manager.
57       *
58       * @param name The name of the appender.
59       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
60       *               they are propagated to the caller.
61       * @param filter The filter, if any, to use.
62       * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
63       *                   the buffer reaches this size.
64       * @param provider The NoSQL provider that provides connections to the chosen NoSQL database.
65       * @return a new NoSQL appender.
66       */
67      @PluginFactory
68      public static NoSQLAppender createAppender(
69              @PluginAttribute("name") final String name,
70              @PluginAttribute("ignoreExceptions") final String ignore,
71              @PluginElement("Filter") final Filter filter,
72              @PluginAttribute("bufferSize") final String bufferSize,
73              @PluginElement("NoSqlProvider") final NoSQLProvider<?> provider) {
74          if (provider == null) {
75              LOGGER.error("NoSQL provider not specified for appender [{}].", name);
76              return null;
77          }
78  
79          final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
80          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
81  
82          final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt
83                  + ", provider=" + provider + " }";
84  
85          final NoSQLDatabaseManager<?> manager = NoSQLDatabaseManager.getNoSQLDatabaseManager(
86                  managerName, bufferSizeInt, provider
87          );
88          if (manager == null) {
89              return null;
90          }
91  
92          return new NoSQLAppender(name, filter, ignoreExceptions, manager);
93      }
94  }