addresses = database.getMongo().getAllAddress();
+ if (addresses.size() == 1) {
+ description += ", server=" + addresses.get(0).getHost() + ", port=" + addresses.get(0).getPort();
+ } else {
+ description += ", servers=[";
+ for (ServerAddress address : addresses) {
+ description += " { " + address.getHost() + ", " + address.getPort() + " } ";
+ }
+ description += "]";
+ }
+ } catch (ClassNotFoundException e) {
+ LOGGER.error("The factory class [{}] could not be loaded.", factoryClassName, e);
+ return null;
+ } catch (NoSuchMethodException e) {
+ LOGGER.error("The factory class [{}] does not have a no-arg method named [{}].",
+ factoryClassName, factoryMethodName, e);
+ return null;
+ } catch (Exception e) {
+ LOGGER.error("The factory method [{}.{}()] could not be invoked.",
+ factoryClassName, factoryMethodName, e);
+ return null;
+ }
+ } else if (databaseName != null && databaseName.length() > 0) {
+ description = "database=" + databaseName;
+ try {
+ if (server != null && server.length() > 0) {
+ int portInt = 0;
+ if (port != null && port.length() > 0) {
+ try {
+ portInt = Integer.parseInt(port);
+ } catch (NumberFormatException ignore) { /* */ }
+ }
+
+ description += ", server=" + server;
+ if (portInt > 0) {
+ description += ", port=" + portInt;
+ database = new MongoClient(server, portInt).getDB(databaseName);
+ } else {
+ database = new MongoClient(server).getDB(databaseName);
+ }
+ } else {
+ database = new MongoClient().getDB(databaseName);
+ }
+ } catch (Exception e) {
+ LOGGER.error("Failed to obtain a database instance from the MongoClient at server [{}] and " +
+ "port [{}].", server, port);
+ return null;
+ }
+ } else {
+ LOGGER.error("No factory method was provided so the database name is required.");
+ return null;
+ }
+
+ if (!database.isAuthenticated()) {
+ if (username != null && username.length() > 0 && password != null && password.length() > 0) {
+ description += ", username=" + username + ", passwordHash=" +
+ NameUtil.md5(password + MongoDBProvider.class.getName());
+ } else {
+ LOGGER.error("The database is not already authenticated so you must supply a username and password " +
+ "for the MongoDB provider.");
+ return null;
+ }
+ }
+
+ WriteConcern writeConcern;
+ if (writeConcernConstant != null && writeConcernConstant.length() > 0) {
+ if (writeConcernConstantClassName != null && writeConcernConstantClassName.length() > 0) {
+ try {
+ Class> writeConcernConstantClass = Class.forName(writeConcernConstantClassName);
+ Field field = writeConcernConstantClass.getField(writeConcernConstant);
+ writeConcern = (WriteConcern) field.get(null);
+ } catch (Exception e) {
+ LOGGER.error("Write concern constant [{}.{}] not found, using default.",
+ writeConcernConstantClassName, writeConcernConstant);
+ writeConcern = WriteConcern.ACKNOWLEDGED;
+ }
+ } else {
+ writeConcern = WriteConcern.valueOf(writeConcernConstant);
+ if (writeConcern == null) {
+ LOGGER.warn("Write concern constant [{}] not found, using default.", writeConcernConstant);
+ writeConcern = WriteConcern.ACKNOWLEDGED;
+ }
+ }
+ } else {
+ writeConcern = WriteConcern.ACKNOWLEDGED;
+ }
+
+ return new MongoDBProvider(database, writeConcern, collectionName, description);
+ }
+}
Index: core/src/main/java/org/apache/logging/log4j/core/appender/db/nosql/mongo/package-info.java
===================================================================
--- core/src/main/java/org/apache/logging/log4j/core/appender/db/nosql/mongo/package-info.java (revision 0)
+++ core/src/main/java/org/apache/logging/log4j/core/appender/db/nosql/mongo/package-info.java (working copy)
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+/**
+ * The classes in this package contain the MongoDB provider for the NoSQL Appender.
+ */
+package org.apache.logging.log4j.core.appender.db.nosql.mongo;
Index: core/src/main/java/org/apache/logging/log4j/core/appender/db/nosql/package-info.java
===================================================================
--- core/src/main/java/org/apache/logging/log4j/core/appender/db/nosql/package-info.java (revision 0)
+++ core/src/main/java/org/apache/logging/log4j/core/appender/db/nosql/package-info.java (working copy)
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+/**
+ * The NoSQL Appender supports writing log events to NoSQL databases. The following NoSQL databases are currently
+ * supported. You can also easily extend this to support other NoSQL databases by implementing just three interfaces:
+ * {@link NoSQLObject}, {@link NoSQLConnection}, and {@link NoSQLProvider}. You will need the client library for your
+ * NoSQL database of choice on the classpath to use this appender; these Maven dependencies are optional and will not
+ * automatically be added to your classpath.
+ *
+ *
+ * - MongoDB: org.mongodb:mongo-java-driver:2.11.1 or newer
+ * must be on the classpath.
+ * - Apache CouchDB: org.lightcouch:lightcouch:0.0.5 or
+ * newer must be on the classpath.
+ *
+ */
+package org.apache.logging.log4j.core.appender.db.nosql;
Index: core/src/main/java/org/apache/logging/log4j/core/appender/db/package-info.java
===================================================================
--- core/src/main/java/org/apache/logging/log4j/core/appender/db/package-info.java (revision 0)
+++ core/src/main/java/org/apache/logging/log4j/core/appender/db/package-info.java (working copy)
@@ -0,0 +1,21 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+/**
+ * The classes in this package and sub packages provide appenders for various types of databases and methods for
+ * accessing databases.
+ */
+package org.apache.logging.log4j.core.appender.db;
Index: core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java
===================================================================
--- core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java (revision 1479389)
+++ core/src/main/java/org/apache/logging/log4j/core/config/BaseConfiguration.java (working copy)
@@ -231,7 +231,7 @@
setName(DefaultConfiguration.DEFAULT_NAME);
final Layout extends Serializable> layout =
PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n",
- null, null, null);
+ null, null, null, null);
final Appender> appender = ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false",
"true");
appender.start();
Index: core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java
===================================================================
--- core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java (revision 1479389)
+++ core/src/main/java/org/apache/logging/log4j/core/config/DefaultConfiguration.java (working copy)
@@ -49,7 +49,7 @@
setName(DEFAULT_NAME);
final Layout extends Serializable> layout =
- PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n", null, null, null);
+ PatternLayout.createLayout("%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n", null, null, null, null);
final Appender extends Serializable> appender =
ConsoleAppender.createAppender(layout, null, "SYSTEM_OUT", "Console", "false", "true");
appender.start();
Index: core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
===================================================================
--- core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java (revision 1479389)
+++ core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java (working copy)
@@ -16,8 +16,6 @@
*/
package org.apache.logging.log4j.core.layout;
-import java.util.HashMap;
-import java.util.Map;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
@@ -33,7 +31,9 @@
import org.apache.logging.log4j.core.pattern.RegexReplacement;
import java.nio.charset.Charset;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
/**
* A flexible layout configurable with pattern string. The goal of this class
@@ -92,6 +92,8 @@
private final RegexReplacement replace;
+ private final boolean handleExceptions;
+
/**
* Constructs a EnhancedPatternLayout using the supplied conversion pattern.
*
@@ -99,15 +101,18 @@
* @param replace The regular expression to match.
* @param pattern conversion pattern.
* @param charset The character set.
+ * @param handleExceptions Whether or not exceptions should always be handled in this pattern (if {@code true},
+ * exceptions will be written even if the pattern does not specify so).
*/
private PatternLayout(final Configuration config, final RegexReplacement replace, final String pattern,
- final Charset charset) {
+ final Charset charset, boolean handleExceptions) {
super(charset);
this.replace = replace;
this.conversionPattern = pattern;
this.config = config;
+ this.handleExceptions = handleExceptions;
final PatternParser parser = createPatternParser(config);
- formatters = parser.parse(pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern, true);
+ formatters = parser.parse(pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern, this.handleExceptions);
}
/**
@@ -123,7 +128,7 @@
return;
}
final PatternParser parser = createPatternParser(this.config);
- formatters = parser.parse(pattern);
+ formatters = parser.parse(pattern, this.handleExceptions);
}
public String getConversionPattern() {
@@ -190,18 +195,24 @@
/**
* Create a pattern layout.
+ *
* @param pattern The pattern. If not specified, defaults to DEFAULT_CONVERSION_PATTERN.
* @param config The Configuration. Some Converters require access to the Interpolator.
* @param replace A Regex replacement String.
* @param charsetName The character set.
+ * @param suppressExceptions Whether or not exceptions should be suppressed in this pattern (defaults to no, which
+ * means exceptions will be written even if the pattern does not specify so).
* @return The PatternLayout.
*/
@PluginFactory
public static PatternLayout createLayout(@PluginAttr("pattern") final String pattern,
@PluginConfiguration final Configuration config,
@PluginElement("replace") final RegexReplacement replace,
- @PluginAttr("charset") final String charsetName) {
+ @PluginAttr("charset") final String charsetName,
+ @PluginAttr("suppressExceptions") final String suppressExceptions) {
final Charset charset = Charsets.getSupportedCharset(charsetName);
- return new PatternLayout(config, replace, pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern, charset);
+ boolean handleExceptions = suppressExceptions == null || !Boolean.parseBoolean(suppressExceptions);
+ return new PatternLayout(config, replace, pattern == null ? DEFAULT_CONVERSION_PATTERN : pattern, charset,
+ handleExceptions);
}
}
Index: core/src/site/xdoc/index.xml
===================================================================
--- core/src/site/xdoc/index.xml (revision 1479389)
+++ core/src/site/xdoc/index.xml (working copy)
@@ -26,29 +26,33 @@
- The Log4Jj 2 implementation provides the functional components
- of the logging system.
- Users are free to create their own plugins and include them
- in the logging configuration.
+ The Log4Jj 2 implementation provides the functional components of the logging system.
+ Users are free to create their own plugins and include them in the logging configuration.
-
- Log4j 2 requires Java 6.
- Some features may require optional
- dependencies. These dependencies are
- specified in the documentation for those features.
-
-
- - JSON configuration requires the Jackson JSON-processor.
- - Async Loggers require the LMAX Disruptor.
- - SMTPAppender requires Javax Mail.
- - JMSQueueAppender and JMSTopicAppender require a JMS implementation like
- Apache ActiveMQ.
- - Windows color support requires Jansi.
-
+
+ Log4j 2 requires Java 6. Some features may require optional
+ dependencies. These dependencies are specified in the
+ documentation for those features.
+
+
+ - JSON configuration requires the Jackson JSON-processor.
+ - Async Loggers require the LMAX Disruptor.
+ - SMTPAppender requires Javax Mail.
+ - JMSQueueAppender and JMSTopicAppender require a JMS implementation like
+ Apache ActiveMQ.
+ - Windows color support requires Jansi.
+ - The JDBC Appender requires a JDBC driver for the database you choese to write events to.
+ - The JPA Appender requires the Java Persistence API classes, a JPA provider implementation,
+ and a decorated entity that the user implements. It also requires an appropriate JDBC driver.
+ - The NoSQL Appender with MongoDB provider requires the MongoDB Java Client driver.
+ - The NoSQL Appender with Apache CouchDB provider requires the LightCouch CouchDB client library.
+ - The NoSQL Appender can be customized with a user-supplied provider, which will require the
+ appropriate client library.
+