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.nosql;
18  
19  import org.apache.logging.log4j.core.Appender;
20  import org.apache.logging.log4j.core.Filter;
21  import org.apache.logging.log4j.core.appender.AbstractAppender;
22  import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
25  import org.apache.logging.log4j.core.config.plugins.PluginElement;
26  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27  import org.apache.logging.log4j.core.util.Booleans;
28  
29  /**
30   * This Appender writes logging events to a NoSQL database using a configured NoSQL provider. It requires
31   * implementations of {@link NoSqlObject}, {@link NoSqlConnection}, and {@link NoSqlProvider} to "know" how to write
32   * events to the chosen NoSQL database.
33   * 
34   * For examples on how to write your own NoSQL provider, see the simple source code for the MongoDB and CouchDB
35   * providers.
36   * </p>
37   * 
38   * @see NoSqlObject
39   * @see NoSqlConnection
40   * @see NoSqlProvider
41   */
42  @Plugin(name = "NoSql", category = "Core", elementType = Appender.ELEMENT_TYPE, printObject = true)
43  public final class NoSqlAppender extends AbstractDatabaseAppender<NoSqlDatabaseManager<?>> {
44      private final String description;
45  
46      private NoSqlAppender(final String name, final Filter filter, final boolean ignoreExceptions,
47                            final NoSqlDatabaseManager<?> manager) {
48          super(name, filter, ignoreExceptions, manager);
49          this.description = this.getName() + "{ manager=" + this.getManager() + " }";
50      }
51  
52      @Override
53      public String toString() {
54          return this.description;
55      }
56  
57      /**
58       * Factory method for creating a NoSQL appender within the plugin manager.
59       *
60       * @param name The name of the appender.
61       * @param ignore If {@code "true"} (default) exceptions encountered when appending events are logged; otherwise
62       *               they are propagated to the caller.
63       * @param filter The filter, if any, to use.
64       * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
65       *                   the buffer reaches this size.
66       * @param provider The NoSQL provider that provides connections to the chosen NoSQL database.
67       * @return a new NoSQL appender.
68       */
69      @PluginFactory
70      public static NoSqlAppender createAppender(
71              @PluginAttribute("name") final String name,
72              @PluginAttribute("ignoreExceptions") final String ignore,
73              @PluginElement("Filter") final Filter filter,
74              @PluginAttribute("bufferSize") final String bufferSize,
75              @PluginElement("NoSqlProvider") final NoSqlProvider<?> provider) {
76          if (provider == null) {
77              LOGGER.error("NoSQL provider not specified for appender [{}].", name);
78              return null;
79          }
80  
81          final int bufferSizeInt = AbstractAppender.parseInt(bufferSize, 0);
82          final boolean ignoreExceptions = Booleans.parseBoolean(ignore, true);
83  
84          final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt
85                  + ", provider=" + provider + " }";
86  
87          final NoSqlDatabaseManager<?> manager = NoSqlDatabaseManager.getNoSqlDatabaseManager(
88                  managerName, bufferSizeInt, provider
89          );
90          if (manager == null) {
91              return null;
92          }
93  
94          return new NoSqlAppender(name, filter, ignoreExceptions, manager);
95      }
96  }