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 */ 017 package org.apache.logging.log4j.core.appender.db.nosql; 018 019 import org.apache.logging.log4j.core.Filter; 020 import org.apache.logging.log4j.core.appender.db.AbstractDatabaseAppender; 021 import org.apache.logging.log4j.core.config.plugins.Plugin; 022 import org.apache.logging.log4j.core.config.plugins.PluginAttr; 023 import org.apache.logging.log4j.core.config.plugins.PluginElement; 024 import org.apache.logging.log4j.core.config.plugins.PluginFactory; 025 026 /** 027 * This Appender writes logging events to a NoSQL database using a configured NoSQL provider. It requires 028 * implementations of {@link NoSQLObject}, {@link NoSQLConnection}, and {@link NoSQLProvider} to "know" how to write 029 * events to the chosen NoSQL database. Two provider implementations are provided: MongoDB 030 * (org.mongodb:mongo-java-driver:2.11.1 or newer must be on the classpath) and Apache CouchDB 031 * (org.lightcouch:lightcouch:0.0.5 or newer must be on the classpath). For examples on how to write your own NoSQL 032 * provider, see the simple source code for the MongoDB and CouchDB providers. 033 * 034 * @see NoSQLObject 035 * @see NoSQLConnection 036 * @see NoSQLProvider 037 */ 038 @Plugin(name = "NoSql", category = "Core", elementType = "appender", printObject = true) 039 public final class NoSQLAppender extends AbstractDatabaseAppender<NoSQLDatabaseManager<?>> { 040 private final String description; 041 042 private NoSQLAppender(final String name, final Filter filter, final boolean handleException, 043 final NoSQLDatabaseManager<?> manager) { 044 super(name, filter, handleException, manager); 045 this.description = this.getName() + "{ manager=" + this.getManager() + " }"; 046 } 047 048 @Override 049 public String toString() { 050 return this.description; 051 } 052 053 /** 054 * Factory method for creating a NoSQL appender within the plugin manager. 055 * 056 * @param name The name of the appender. 057 * @param suppressExceptions {@code "true"} (default) if logging exceptions should be hidden from the application, 058 * {@code "false"} otherwise. 059 * @param filter The filter, if any, to use. 060 * @param bufferSize If an integer greater than 0, this causes the appender to buffer log events and flush whenever 061 * the buffer reaches this size. 062 * @param provider The NoSQL provider that provides connections to the chosen NoSQL database. 063 * @return a new NoSQL appender. 064 */ 065 @PluginFactory 066 public static NoSQLAppender createAppender(@PluginAttr("name") final String name, 067 @PluginAttr("suppressExceptions") final String suppressExceptions, 068 @PluginElement("filter") final Filter filter, 069 @PluginAttr("bufferSize") final String bufferSize, 070 @PluginElement("noSqlProvider") final NoSQLProvider<?> provider) { 071 if (provider == null) { 072 LOGGER.error("NoSQL provider not specified for appender [{}].", name); 073 return null; 074 } 075 076 int bufferSizeInt; 077 try { 078 bufferSizeInt = bufferSize == null || bufferSize.length() == 0 ? 0 : Integer.parseInt(bufferSize); 079 } catch (final NumberFormatException e) { 080 LOGGER.warn("Buffer size [" + bufferSize + "] not an integer, using no buffer."); 081 bufferSizeInt = 0; 082 } 083 084 final boolean handleExceptions = suppressExceptions == null || !Boolean.parseBoolean(suppressExceptions); 085 086 final String managerName = "noSqlManager{ description=" + name + ", bufferSize=" + bufferSizeInt 087 + ", provider=" + provider + " }"; 088 089 final NoSQLDatabaseManager<?> manager = NoSQLDatabaseManager.getNoSQLDatabaseManager( 090 managerName, bufferSizeInt, provider 091 ); 092 if (manager == null) { 093 return null; 094 } 095 096 return new NoSQLAppender(name, filter, handleExceptions, manager); 097 } 098 }