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.db.AbstractDatabaseAppender;
21  import org.apache.logging.log4j.core.config.plugins.Plugin;
22  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
23  import org.apache.logging.log4j.core.config.plugins.PluginElement;
24  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
25  
26  /**
27   * This Appender writes logging events to a NoSQL database using a configured NoSQL provider. It requires
28   * implementations of {@link NoSQLObject}, {@link NoSQLConnection}, and {@link NoSQLProvider} to "know" how to write
29   * events to the chosen NoSQL database. Two provider implementations are provided: MongoDB
30   * (org.mongodb:mongo-java-driver:2.11.1 or newer must be on the classpath) and Apache CouchDB
31   * (org.lightcouch:lightcouch:0.0.5 or newer must be on the classpath). For examples on how to write your own NoSQL
32   * provider, see the simple source code for the MongoDB and CouchDB providers.
33   * 
34   * @see NoSQLObject
35   * @see NoSQLConnection
36   * @see NoSQLProvider
37   */
38  @Plugin(name = "NoSql", category = "Core", elementType = "appender", printObject = true)
39  public final class NoSQLAppender extends AbstractDatabaseAppender<NoSQLDatabaseManager<?>> {
40      private final String description;
41  
42      private NoSQLAppender(final String name, final Filter filter, final boolean handleException,
43                            final NoSQLDatabaseManager<?> manager) {
44          super(name, filter, handleException, manager);
45          this.description = this.getName() + "{ manager=" + this.getManager() + " }";
46      }
47  
48      @Override
49      public String toString() {
50          return this.description;
51      }
52  
53      /**
54       * Factory method for creating a NoSQL appender within the plugin manager.
55       *
56       * @param name The name of the appender.
57       * @param suppressExceptions {@code "true"} (default) if logging exceptions should be hidden from the application,
58       *                           {@code "false"} otherwise.
59       * @param filter The filter, if any, to use.
60       * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever
61       *                   the buffer reaches this size.
62       * @param provider The NoSQL provider that provides connections to the chosen NoSQL database.
63       * @return a new NoSQL appender.
64       */
65      @PluginFactory
66      public static NoSQLAppender createAppender(@PluginAttr("name") final String name,
67                                                 @PluginAttr("suppressExceptions") final String suppressExceptions,
68                                                 @PluginElement("filter") final Filter filter,
69                                                 @PluginAttr("bufferSize") final String bufferSize,
70                                                 @PluginElement("noSqlProvider") final NoSQLProvider<?> provider) {
71          if (provider == null) {
72              LOGGER.error("NoSQL provider not specified for appender [{}].", name);
73              return null;
74          }
75  
76          int bufferSizeInt;
77          try {
78              bufferSizeInt = bufferSize == null || bufferSize.length() == 0 ? 0 : Integer.parseInt(bufferSize);
79          } catch (final NumberFormatException e) {
80              LOGGER.warn("Buffer size [" + bufferSize + "] not an integer, using no buffer.");
81              bufferSizeInt = 0;
82          }
83  
84          final boolean handleExceptions = suppressExceptions == null || !Boolean.parseBoolean(suppressExceptions);
85  
86          final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt
87                  + ", provider=" + provider + " }";
88  
89          final NoSQLDatabaseManager<?> manager = NoSQLDatabaseManager.getNoSQLDatabaseManager(
90                  managerName, bufferSizeInt, provider
91          );
92          if (manager == null) {
93              return null;
94          }
95  
96          return new NoSQLAppender(name, filter, handleExceptions, manager);
97      }
98  }