[
https://issues.apache.org/jira/browse/LOG4J2-1864?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16080847#comment-16080847
]
ASF GitHub Bot commented on LOG4J2-1864:
----------------------------------------
Github user codescale commented on a diff in the pull request:
https://github.com/apache/logging-log4j2/pull/62#discussion_r126504007
--- Diff:
log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java
---
@@ -93,130 +103,249 @@ public String toString() {
* @param factoryMethodName The name of the public static factory
method belonging to the aforementioned factory
* class.
* @return a new MongoDB provider.
+ * @deprecated in 2.8; use {@link #newBuilder()} instead.
*/
@PluginFactory
public static MongoDbProvider createNoSqlProvider(
- @PluginAttribute("collectionName") final String collectionName,
- @PluginAttribute("writeConcernConstant") final String
writeConcernConstant,
- @PluginAttribute("writeConcernConstantClass") final String
writeConcernConstantClassName,
- @PluginAttribute("databaseName") final String databaseName,
- @PluginAttribute(value = "server", defaultString =
"localhost") @ValidHost final String server,
- @PluginAttribute(value = "port", defaultString = "" +
DEFAULT_PORT) @ValidPort final String port,
- @PluginAttribute("userName") final String userName,
- @PluginAttribute(value = "password", sensitive = true) final
String password,
- @PluginAttribute("factoryClassName") final String
factoryClassName,
- @PluginAttribute("factoryMethodName") final String
factoryMethodName) {
- DB database;
- String description;
- if (Strings.isNotEmpty(factoryClassName) &&
Strings.isNotEmpty(factoryMethodName)) {
- try {
- final Class<?> factoryClass =
LoaderUtil.loadClass(factoryClassName);
- final Method method =
factoryClass.getMethod(factoryMethodName);
- final Object object = method.invoke(null);
-
- if (object instanceof DB) {
- database = (DB) object;
- } else if (object instanceof MongoClient) {
- if (Strings.isNotEmpty(databaseName)) {
- database = ((MongoClient)
object).getDB(databaseName);
- } else {
- LOGGER.error("The factory method [{}.{}()]
returned a MongoClient so the database name is "
- + "required.", factoryClassName,
factoryMethodName);
- return null;
- }
- } else if (object == null) {
- LOGGER.error("The factory method [{}.{}()] returned
null.", factoryClassName, factoryMethodName);
- return null;
- } else {
- LOGGER.error("The factory method [{}.{}()] returned an
unsupported type [{}].", factoryClassName,
- factoryMethodName,
object.getClass().getName());
- return null;
- }
-
- description = "database=" + database.getName();
- final List<ServerAddress> addresses =
database.getMongo().getAllAddress();
- if (addresses.size() == 1) {
- description += ", server=" +
addresses.get(0).getHost() + ", port=" + addresses.get(0).getPort();
- } else {
- description += ", servers=[";
- for (final ServerAddress address : addresses) {
- description += " { " + address.getHost() + ", " +
address.getPort() + " } ";
- }
- description += "]";
- }
- } catch (final ClassNotFoundException e) {
- LOGGER.error("The factory class [{}] could not be
loaded.", factoryClassName, e);
- return null;
- } catch (final NoSuchMethodException e) {
- LOGGER.error("The factory class [{}] does not have a
no-arg method named [{}].", factoryClassName,
- factoryMethodName, e);
- return null;
- } catch (final Exception e) {
- LOGGER.error("The factory method [{}.{}()] could not be
invoked.", factoryClassName, factoryMethodName,
- e);
- return null;
- }
- } else if (Strings.isNotEmpty(databaseName)) {
- final List<MongoCredential> credentials = new ArrayList<>();
- description = "database=" + databaseName;
- if (Strings.isNotEmpty(userName) &&
Strings.isNotEmpty(password)) {
- description += ", username=" + userName + ", passwordHash="
- + NameUtil.md5(password +
MongoDbProvider.class.getName());
- credentials.add(MongoCredential.createCredential(userName,
databaseName, password.toCharArray()));
- }
- try {
- final int portInt = TypeConverters.convert(port,
int.class, DEFAULT_PORT);
- description += ", server=" + server + ", port=" + portInt;
- database = new MongoClient(new ServerAddress(server,
portInt), credentials).getDB(databaseName);
- } catch (final 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;
- }
-
- try {
- database.getCollectionNames(); // Check if the database
actually requires authentication
- } catch (final Exception e) {
- LOGGER.error(
- "The database is not up, or you are not authenticated,
try supplying a username and password to the MongoDB provider.",
- e);
- return null;
- }
-
- final WriteConcern writeConcern =
toWriteConcern(writeConcernConstant, writeConcernConstantClassName);
-
- return new MongoDbProvider(database, writeConcern, collectionName,
description);
- }
+ final String collectionName,
+ final String writeConcernConstant,
+ final String writeConcernConstantClassName,
+ final String databaseName,
+ final String server,
+ final String port,
+ final String userName,
+ final String password,
+ final String factoryClassName,
+ final String factoryMethodName) {
+ LOGGER.info("createNoSqlProvider");
+ return
newBuilder().setCollectionName(collectionName).setWriteConcernConstant(writeConcernConstantClassName)
+
.setWriteConcernConstant(writeConcernConstant).setDatabaseName(databaseName).setServer(server)
+
.setPort(port).setUserName(userName).setPassword(password).setFactoryClassName(factoryClassName)
+
.setFactoryMethodName(factoryMethodName).build();
+ }
+
+ @PluginBuilderFactory
+ public static <B extends Builder<B>> B newBuilder() {
+ return new Builder<B>().asBuilder();
+ }
+
+ public static class Builder<B extends Builder<B>> extends
AbstractFilterable.Builder<B>
+ implements
org.apache.logging.log4j.core.util.Builder<MongoDbProvider> {
+
+ @PluginBuilderAttribute
+ @ValidHost
+ private String server = "localhost";
+
+ @PluginBuilderAttribute
+ @ValidPort
+ private String port = "" + DEFAULT_PORT;
+
+ @PluginBuilderAttribute
+ @Required(message = "No database name provided")
+ private String databaseName;
+
+ @PluginBuilderAttribute
+ @Required(message = "No collection name provided")
+ private String collectionName;
+
+ @PluginBuilderAttribute
+ private String userName;
+
+ @PluginBuilderAttribute(sensitive = true)
+ private String password;
+
+ @PluginBuilderAttribute("capped")
+ private boolean isCapped = false;
+
+ @PluginBuilderAttribute
+ private int collectionSize = DEFAULT_COLLECTION_SIZE;
+
+ @PluginBuilderAttribute
+ private String factoryClassName;
+
+ @PluginBuilderAttribute
+ private String factoryMethodName;
+
+ @PluginBuilderAttribute
+ private String writeConcernConstantClassName;
+
+ @PluginBuilderAttribute
+ private String writeConcernConstant;
+
+ public B setServer(String server) {
+ this.server = server;
+ return asBuilder();
+ }
+
+ public B setPort(String port) {
+ this.port = port;
+ return asBuilder();
+ }
+
+ public B setDatabaseName(String databaseName) {
+ this.databaseName = databaseName;
+ return asBuilder();
+ }
+
+ public B setCollectionName(String collectionName) {
+ this.collectionName = collectionName;
+ return asBuilder();
+ }
+
+ public B setUserName(String userName) {
+ this.userName = userName;
+ return asBuilder();
+ }
+
+ public B setPassword(String password) {
+ this.password = password;
+ return asBuilder();
+ }
+
+ public B setCapped(boolean isCapped) {
+ this.isCapped = isCapped;
+ return asBuilder();
+ }
+
+ public B setCollectionSize(int collectionSize) {
+ this.collectionSize = collectionSize;
+ return asBuilder();
+ }
+
+ public B setFactoryClassName(String factoryClassName) {
+ this.factoryClassName = factoryClassName;
+ return asBuilder();
+ }
+
+ public B setFactoryMethodName(String factoryMethodName) {
+ this.factoryMethodName = factoryMethodName;
+ return asBuilder();
+ }
+
+ public B setWriteConcernConstantClassName(String
writeConcernConstantClassName) {
+ this.writeConcernConstantClassName =
writeConcernConstantClassName;
+ return asBuilder();
+ }
+
+ public B setWriteConcernConstant(String writeConcernConstant) {
+ this.writeConcernConstant = writeConcernConstant;
+ return asBuilder();
+ }
+
+ @Override
+ public MongoDbProvider build() {
+ DB database;
+ String description;
+ if (Strings.isNotEmpty(factoryClassName) &&
Strings.isNotEmpty(factoryMethodName)) {
+ try {
+ final Class<?> factoryClass =
LoaderUtil.loadClass(factoryClassName);
+ final Method method =
factoryClass.getMethod(factoryMethodName);
+ final Object object = method.invoke(null);
+
+ if (object instanceof DB) {
+ database = (DB) object;
+ } else if (object instanceof MongoClient) {
+ if (Strings.isNotEmpty(databaseName)) {
+ database = ((MongoClient)
object).getDB(databaseName);
+ } else {
+ LOGGER.error("The factory method [{}.{}()]
returned a MongoClient so the database name is "
+ + "required.", factoryClassName,
factoryMethodName);
+ return null;
+ }
+ } else if (object == null) {
+ LOGGER.error("The factory method [{}.{}()] returned
null.", factoryClassName, factoryMethodName);
+ return null;
+ } else {
+ LOGGER.error("The factory method [{}.{}()] returned
an unsupported type [{}].", factoryClassName,
+ factoryMethodName,
object.getClass().getName());
+ return null;
+ }
+
+ description = "database=" + database.getName();
+ final List<ServerAddress> addresses =
database.getMongo().getAllAddress();
+ if (addresses.size() == 1) {
+ description += ", server=" +
addresses.get(0).getHost() + ", port=" + addresses.get(0).getPort();
+ } else {
+ description += ", servers=[";
+ for (final ServerAddress address : addresses) {
+ description += " { " + address.getHost() + ", "
+ address.getPort() + " } ";
+ }
+ description += "]";
+ }
+ } catch (final ClassNotFoundException e) {
+ LOGGER.error("The factory class [{}] could not be
loaded.", factoryClassName, e);
+ return null;
+ } catch (final NoSuchMethodException e) {
+ LOGGER.error("The factory class [{}] does not have a
no-arg method named [{}].", factoryClassName,
+ factoryMethodName, e);
+ return null;
+ } catch (final Exception e) {
+ LOGGER.error("The factory method [{}.{}()] could not be
invoked.", factoryClassName, factoryMethodName,
+ e);
+ return null;
+ }
+ } else if (Strings.isNotEmpty(databaseName)) {
+ final List<MongoCredential> credentials = new ArrayList<>();
+ description = "database=" + databaseName;
+ if (Strings.isNotEmpty(userName) &&
Strings.isNotEmpty(password)) {
+ description += ", username=" + userName + ",
passwordHash="
+ + NameUtil.md5(password +
MongoDbProvider.class.getName());
+
credentials.add(MongoCredential.createCredential(userName, databaseName,
password.toCharArray()));
+ }
+ try {
+ final int portInt = TypeConverters.convert(port,
int.class, DEFAULT_PORT);
--- End diff --
https://github.com/codescale/logging-log4j2/blob/master/log4j-nosql/src/main/java/org/apache/logging/log4j/nosql/appender/mongodb/MongoDbProvider.java#L296
> Support capped collection for MongoDB Log-Provider
> --------------------------------------------------
>
> Key: LOG4J2-1864
> URL: https://issues.apache.org/jira/browse/LOG4J2-1864
> Project: Log4j 2
> Issue Type: New Feature
> Components: Appenders
> Reporter: Matt
>
> MongoDB supports sth. called capped collections. If the
> nosql-mongodb-appender supports this feature, the mongodb-collection could
> never "overflow" and stick to a defined maximum size.
> see [pull request 62|https://github.com/apache/logging-log4j2/pull/62] for
> more details.
--
This message was sent by Atlassian JIRA
(v6.4.14#64029)