You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2025/05/31 09:53:31 UTC
(logging-log4j2) branch jesmith17-main-mongo-dbname created (now b9006b5cad)
This is an automated email from the ASF dual-hosted git repository.
vy pushed a change to branch jesmith17-main-mongo-dbname
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
at b9006b5cad Add `collectionName` and `databaseName` attributes to `MongoDbProvider`
This branch includes the following new commits:
new b9006b5cad Add `collectionName` and `databaseName` attributes to `MongoDbProvider`
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(logging-log4j2) 01/01: Add `collectionName` and `databaseName` attributes to `MongoDbProvider`
Posted by vy...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch jesmith17-main-mongo-dbname
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit b9006b5cad4b4acc0570702e1752d5246a3710c4
Author: Josh Smith <je...@gmail.com>
AuthorDate: Sat Dec 21 17:40:55 2024 -0600
Add `collectionName` and `databaseName` attributes to `MongoDbProvider`
---
log4j-mongodb/pom.xml | 5 +-
.../logging/log4j/mongodb/MongoDbConnection.java | 12 +--
.../logging/log4j/mongodb/MongoDbProvider.java | 72 +++++++++++--
.../log4j/mongodb/MongoDbCollectionNameIT.java | 36 +++++++
.../MongoDbDatabaseAndCollectionNameIT.java | 36 +++++++
.../logging/log4j/mongodb/MongoDbProviderTest.java | 112 +++++++++++++++++++++
.../src/test/resources/MongoDbAdditionalFields.xml | 5 +-
.../src/test/resources/MongoDbAuthFailureIT.xml | 4 +-
.../src/test/resources/MongoDbCappedIntIT.xml | 4 +-
.../src/test/resources/MongoDbCappedLongIT.xml | 4 +-
...CappedIntIT.xml => MongoDbCollectionNameIT.xml} | 7 +-
....xml => MongoDbDatabaseAndCollectionNameIT.xml} | 8 +-
log4j-mongodb/src/test/resources/MongoDbIT.xml | 2 +-
.../src/test/resources/MongoDbMapMessageIT.xml | 7 +-
src/changelog/.3.x.x/update_org_log4j_mongodb.xml | 8 ++
.../appenders/database/nosql-mongo-keys.json | 4 +-
.../appenders/database/nosql-mongo-keys.properties | 4 +-
.../manual/appenders/database/nosql-mongo-keys.xml | 2 +-
.../appenders/database/nosql-mongo-keys.yaml | 4 +-
.../manual/appenders/database/nosql-mongo.json | 4 +-
.../appenders/database/nosql-mongo.properties | 4 +-
.../manual/appenders/database/nosql-mongo.xml | 2 +-
.../manual/appenders/database/nosql-mongo.yaml | 4 +-
.../ROOT/pages/manual/appenders/database.adoc | 17 ++++
24 files changed, 324 insertions(+), 43 deletions(-)
diff --git a/log4j-mongodb/pom.xml b/log4j-mongodb/pom.xml
index dfd5b9529a..70935cb353 100644
--- a/log4j-mongodb/pom.xml
+++ b/log4j-mongodb/pom.xml
@@ -141,7 +141,6 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
-
</dependencies>
<build>
@@ -150,9 +149,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
- <configuration>
- <skip>true</skip>
- </configuration>
+
<dependencies>
<dependency>
diff --git a/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbConnection.java b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbConnection.java
index cabc6c73e1..0507d8b4b4 100644
--- a/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbConnection.java
+++ b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbConnection.java
@@ -16,7 +16,6 @@
*/
package org.apache.logging.log4j.mongodb;
-import com.mongodb.ConnectionString;
import com.mongodb.MongoException;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoCollection;
@@ -59,20 +58,17 @@ public final class MongoDbConnection extends AbstractNoSqlConnection<Document, M
}
}
- private final ConnectionString connectionString;
private final MongoCollection<Document> collection;
private final MongoClient mongoClient;
public MongoDbConnection(
- final ConnectionString connectionString,
final MongoClient mongoClient,
final MongoDatabase mongoDatabase,
+ final String collectionName,
final boolean isCapped,
final Long sizeInBytes) {
- this.connectionString = connectionString;
this.mongoClient = mongoClient;
- this.collection =
- getOrCreateMongoCollection(mongoDatabase, connectionString.getCollection(), isCapped, sizeInBytes);
+ this.collection = getOrCreateMongoCollection(mongoDatabase, collectionName, isCapped, sizeInBytes);
}
@Override
@@ -106,8 +102,6 @@ public final class MongoDbConnection extends AbstractNoSqlConnection<Document, M
@Override
public String toString() {
- return String.format(
- "Mongo4Connection [connectionString=%s, collection=%s, mongoClient=%s]",
- connectionString, collection, mongoClient);
+ return String.format("Mongo4Connection [collection=%s, mongoClient=%s]", collection, mongoClient);
}
}
diff --git a/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbProvider.java b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbProvider.java
index 8bc0098809..19feceaec2 100644
--- a/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbProvider.java
+++ b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbProvider.java
@@ -18,6 +18,7 @@ package org.apache.logging.log4j.mongodb;
import com.mongodb.ConnectionString;
import com.mongodb.MongoClientSettings;
+import com.mongodb.MongoNamespace;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoDatabase;
@@ -54,9 +55,45 @@ public final class MongoDbProvider implements NoSqlProvider<MongoDbConnection> {
@PluginAttribute("capped")
private boolean capped = false;
+ @PluginAttribute("collectionName")
+ private String collectionName;
+
+ @PluginAttribute("databaseName")
+ private String databaseName;
+
@Override
public MongoDbProvider build() {
- return new MongoDbProvider(connectionStringSource, capped, collectionSize);
+
+ LOGGER.debug("Creating ConnectionString {}...", connectionStringSource);
+ final ConnectionString connectionString;
+ try {
+ connectionString = new ConnectionString(connectionStringSource);
+ } catch (final IllegalArgumentException exception) {
+ final String message = String.format("Invalid MongoDB connection string `%s`.", connectionStringSource);
+ throw new IllegalArgumentException(message, exception);
+ }
+
+ // Validate the provided databaseName property
+ final String effectiveDatabaseName = databaseName != null ? databaseName : connectionString.getDatabase();
+ try {
+ MongoNamespace.checkDatabaseNameValidity(effectiveDatabaseName);
+ } catch (final IllegalArgumentException exception) {
+ final String message = String.format("Invalid MongoDB database name `%s`.", effectiveDatabaseName);
+ throw new IllegalArgumentException(message, exception);
+ }
+
+ // Validate the provided collectionName property
+ final String effectiveCollectionName =
+ collectionName != null ? collectionName : connectionString.getCollection();
+ try {
+ MongoNamespace.checkCollectionNameValidity(effectiveCollectionName);
+ } catch (final IllegalArgumentException exception) {
+ final String message = String.format("Invalid MongoDB collection name `%s`.", effectiveCollectionName);
+ throw new IllegalArgumentException(message, exception);
+ }
+
+ return new MongoDbProvider(
+ connectionString, capped, collectionSize, effectiveDatabaseName, effectiveCollectionName);
}
public B setConnectionStringSource(final String connectionStringSource) {
@@ -73,6 +110,16 @@ public final class MongoDbProvider implements NoSqlProvider<MongoDbConnection> {
this.collectionSize = collectionSize;
return asBuilder();
}
+
+ public B setCollectionName(final String collectionName) {
+ this.collectionName = collectionName;
+ return asBuilder();
+ }
+
+ public B setDatabaseName(final String databaseName) {
+ this.databaseName = databaseName;
+ return asBuilder();
+ }
}
private static final Logger LOGGER = StatusLogger.getLogger();
@@ -94,18 +141,24 @@ public final class MongoDbProvider implements NoSqlProvider<MongoDbConnection> {
private final Long collectionSize;
private final boolean isCapped;
+ private final String collectionName;
private final MongoClient mongoClient;
private final MongoDatabase mongoDatabase;
private final ConnectionString connectionString;
- private MongoDbProvider(final String connectionStringSource, final boolean isCapped, final Long collectionSize) {
- LOGGER.debug("Creating ConnectionString {}...", connectionStringSource);
- this.connectionString = new ConnectionString(connectionStringSource);
+ private MongoDbProvider(
+ final ConnectionString connectionString,
+ final boolean isCapped,
+ final Long collectionSize,
+ final String databaseName,
+ final String collectionName) {
+
LOGGER.debug("Created ConnectionString {}", connectionString);
+ this.connectionString = connectionString;
LOGGER.debug("Creating MongoClientSettings...");
// @formatter:off
final MongoClientSettings settings = MongoClientSettings.builder()
- .applyConnectionString(this.connectionString)
+ .applyConnectionString(connectionString)
.codecRegistry(CODEC_REGISTRIES)
.build();
// @formatter:on
@@ -113,28 +166,29 @@ public final class MongoDbProvider implements NoSqlProvider<MongoDbConnection> {
LOGGER.debug("Creating MongoClient {}...", settings);
this.mongoClient = MongoClients.create(settings);
LOGGER.debug("Created MongoClient {}", mongoClient);
- final String databaseName = this.connectionString.getDatabase();
LOGGER.debug("Getting MongoDatabase {}...", databaseName);
this.mongoDatabase = this.mongoClient.getDatabase(databaseName);
LOGGER.debug("Got MongoDatabase {}", mongoDatabase);
+ this.collectionName = collectionName;
this.isCapped = isCapped;
this.collectionSize = collectionSize;
}
@Override
public MongoDbConnection getConnection() {
- return new MongoDbConnection(connectionString, mongoClient, mongoDatabase, isCapped, collectionSize);
+ return new MongoDbConnection(mongoClient, mongoDatabase, collectionName, isCapped, collectionSize);
}
@Override
public String toString() {
return String.format(
- "%s [connectionString=%s, collectionSize=%s, isCapped=%s, mongoClient=%s, mongoDatabase=%s]",
+ "%s [connectionString=%s, collectionSize=%s, isCapped=%s, mongoClient=%s, mongoDatabase=%s, collectionName=%s]",
MongoDbProvider.class.getSimpleName(),
connectionString,
collectionSize,
isCapped,
mongoClient,
- mongoDatabase);
+ mongoDatabase,
+ collectionName);
}
}
diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCollectionNameIT.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCollectionNameIT.java
new file mode 100644
index 0000000000..d299fddc36
--- /dev/null
+++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCollectionNameIT.java
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+package org.apache.logging.log4j.mongodb;
+
+import com.mongodb.client.MongoClient;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+
+@UsingMongoDb
+@LoggerContextSource("MongoDbCollectionNameIT.xml")
+// Print debug status logger output upon failure
+@UsingStatusListener
+class MongoDbCollectionNameIT extends AbstractMongoDbCappedIT {
+
+ @Test
+ @Override
+ protected void test(final LoggerContext ctx, final MongoClient mongoClient) {
+ super.test(ctx, mongoClient);
+ }
+}
diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbDatabaseAndCollectionNameIT.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbDatabaseAndCollectionNameIT.java
new file mode 100644
index 0000000000..a72c997823
--- /dev/null
+++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbDatabaseAndCollectionNameIT.java
@@ -0,0 +1,36 @@
+/*
+ * 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.
+ */
+package org.apache.logging.log4j.mongodb;
+
+import com.mongodb.client.MongoClient;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+
+@UsingMongoDb
+@LoggerContextSource("MongoDbDatabaseAndCollectionNameIT.xml")
+// Print debug status logger output upon failure
+@UsingStatusListener
+class MongoDbDatabaseAndCollectionNameIT extends AbstractMongoDbCappedIT {
+
+ @Test
+ @Override
+ protected void test(final LoggerContext ctx, final MongoClient mongoClient) {
+ super.test(ctx, mongoClient);
+ }
+}
diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbProviderTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbProviderTest.java
new file mode 100644
index 0000000000..59346e979c
--- /dev/null
+++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbProviderTest.java
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+package org.apache.logging.log4j.mongodb;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import com.mongodb.MongoNamespace;
+import com.mongodb.client.MongoCollection;
+import java.lang.reflect.Field;
+import org.bson.Document;
+import org.junit.jupiter.api.Test;
+
+class MongoDbProviderTest {
+
+ private static final String CS_WO_DB = "mongodb://localhost:27017";
+ private static final String CS_W_DB = "mongodb://localhost:27017/logging";
+ private static final String CS_W_DB_N_COLL = "mongodb://localhost:27017/logging.logs";
+
+ private static final String COLL_NAME = "logsTest";
+ private static final String DB_NAME = "loggingTest";
+
+ @Test
+ void createProviderWithDatabaseAndCollectionProvidedViaConfig() {
+ final MongoDbProvider provider = MongoDbProvider.newBuilder()
+ .setConnectionStringSource(CS_WO_DB)
+ .setDatabaseName(DB_NAME)
+ .setCollectionName(COLL_NAME)
+ .build();
+ assertNotNull(provider);
+ final MongoNamespace namespace = getNamespace(provider.getConnection());
+ assertEquals(COLL_NAME, namespace.getCollectionName());
+ assertEquals(DB_NAME, namespace.getDatabaseName());
+ }
+
+ @Test
+ void createProviderWithoutDatabaseName() {
+ final MongoDbProvider provider =
+ MongoDbProvider.newBuilder().setConnectionStringSource(CS_WO_DB).build();
+ assertNull(provider);
+ }
+
+ @Test
+ void createProviderWithoutDatabaseNameWithCollectionName() {
+ final MongoDbProvider provider = MongoDbProvider.newBuilder()
+ .setConnectionStringSource(CS_WO_DB)
+ .setCollectionName(COLL_NAME)
+ .build();
+ assertNull(provider);
+ }
+
+ @Test
+ void createProviderWithoutCollectionName() {
+ final MongoDbProvider provider = MongoDbProvider.newBuilder()
+ .setConnectionStringSource(CS_WO_DB)
+ .setDatabaseName(DB_NAME)
+ .build();
+ assertNull(provider);
+ }
+
+ @Test
+ void createProviderWithDatabaseOnConnectionString() {
+ final MongoDbProvider provider = MongoDbProvider.newBuilder()
+ .setConnectionStringSource(CS_W_DB)
+ .setCollectionName(COLL_NAME)
+ .build();
+ assertNotNull(provider);
+ final MongoNamespace namespace = getNamespace(provider.getConnection());
+ assertEquals(COLL_NAME, namespace.getCollectionName());
+ assertEquals("logging", namespace.getDatabaseName());
+ }
+
+ @Test
+ void createProviderConfigOverridesConnectionString() {
+ final MongoDbProvider provider = MongoDbProvider.newBuilder()
+ .setConnectionStringSource(CS_W_DB_N_COLL)
+ .setCollectionName(COLL_NAME)
+ .setDatabaseName(DB_NAME)
+ .build();
+ assertNotNull(provider);
+ final MongoNamespace namespace = getNamespace(provider.getConnection());
+ assertEquals(COLL_NAME, namespace.getCollectionName());
+ assertEquals(DB_NAME, namespace.getDatabaseName());
+ }
+
+ private static MongoNamespace getNamespace(final MongoDbConnection connection) {
+ try {
+ final Field collectionField = MongoDbConnection.class.getDeclaredField("collection");
+ collectionField.setAccessible(true);
+ @SuppressWarnings("unchecked")
+ final MongoCollection<Document> collection = (MongoCollection<Document>) collectionField.get(connection);
+ return collection.getNamespace();
+ } catch (final Exception exception) {
+ throw new RuntimeException(exception);
+ }
+ }
+}
diff --git a/log4j-mongodb/src/test/resources/MongoDbAdditionalFields.xml b/log4j-mongodb/src/test/resources/MongoDbAdditionalFields.xml
index 600296753e..3af3f40723 100644
--- a/log4j-mongodb/src/test/resources/MongoDbAdditionalFields.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbAdditionalFields.xml
@@ -22,7 +22,10 @@
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
<Appenders>
<NoSql name="MONGO">
- <MongoDb connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbAdditionalFieldsIT"/>
+ <MongoDb
+ connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
+ databaseName="testDb"
+ collectionName="MongoDbAdditionalFieldsIT"/>
<KeyValuePair key="A" value="1"/>
<KeyValuePair key="B" value="2"/>
<KeyValuePair key="env1" value="${env:PATH}"/>
diff --git a/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml b/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml
index 797dfb47ed..26d9bc8e2f 100644
--- a/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml
@@ -23,7 +23,9 @@
<Appenders>
<NoSql name="MONGO">
<MongoDb
- connection="mongodb://log4jUser:12345678@localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbAuthFailureIT" />
+ connection="mongodb://log4jUser:12345678@localhost:${sys:log4j.mongo.port:-27017}"
+ databaseName="testDb"
+ collectionName="MongoDbAuthFailureIT" />
</NoSql>
</Appenders>
<Loggers>
diff --git a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml b/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
index f1574d396a..645cac894a 100644
--- a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
@@ -23,7 +23,9 @@
<Appenders>
<NoSql name="MONGO">
<MongoDb
- connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbCappedIntIT"
+ connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
+ databaseName="testDb"
+ collectionName="MongoDbCappedIntIT"
capped="true"
collectionSize="1073741824"/>
</NoSql>
diff --git a/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml b/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml
index 0ac524fdea..16de25d261 100644
--- a/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml
@@ -24,7 +24,9 @@
<NoSql name="MONGO">
<!-- collectionSize="2147483657" is max int + 10 -->
<MongoDb
- connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbCappedLongIT"
+ connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
+ databaseName="testDb"
+ collectionName="MongoDbCappedLongIT"
capped="true"
collectionSize="2147483657"/>
</NoSql>
diff --git a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml b/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml
similarity index 91%
copy from log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
copy to log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml
index f1574d396a..4e1a851139 100644
--- a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml
@@ -23,14 +23,15 @@
<Appenders>
<NoSql name="MONGO">
<MongoDb
- connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbCappedIntIT"
+ connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb"
capped="true"
- collectionSize="1073741824"/>
+ collectionSize="1073741824"
+ collectionName="MongoDbCollectionNameIT"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="ALL">
- <AppenderRef ref="MONGO" />
+ <AppenderRef ref="MONGO"/>
</Root>
</Loggers>
</Configuration>
diff --git a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml b/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml
similarity index 89%
copy from log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
copy to log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml
index f1574d396a..53f81ecdeb 100644
--- a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml
@@ -23,14 +23,16 @@
<Appenders>
<NoSql name="MONGO">
<MongoDb
- connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbCappedIntIT"
+ connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
capped="true"
- collectionSize="1073741824"/>
+ collectionSize="1073741824"
+ databaseName="testDb"
+ collectionName="MongoDbDatabaseAndCollectionNameIT"/>
</NoSql>
</Appenders>
<Loggers>
<Root level="ALL">
- <AppenderRef ref="MONGO" />
+ <AppenderRef ref="MONGO"/>
</Root>
</Loggers>
</Configuration>
diff --git a/log4j-mongodb/src/test/resources/MongoDbIT.xml b/log4j-mongodb/src/test/resources/MongoDbIT.xml
index df713f0179..2ed62df6fe 100644
--- a/log4j-mongodb/src/test/resources/MongoDbIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbIT.xml
@@ -22,7 +22,7 @@
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
<Appenders>
<NoSql name="MONGO">
- <MongoDb connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbIT" />
+ <MongoDb connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}" collectionName="MongoDbIT" databaseName="testDb" />
</NoSql>
</Appenders>
<Loggers>
diff --git a/log4j-mongodb/src/test/resources/MongoDbMapMessageIT.xml b/log4j-mongodb/src/test/resources/MongoDbMapMessageIT.xml
index 774fa4bfae..3e35f30fdf 100644
--- a/log4j-mongodb/src/test/resources/MongoDbMapMessageIT.xml
+++ b/log4j-mongodb/src/test/resources/MongoDbMapMessageIT.xml
@@ -22,13 +22,16 @@
https://logging.apache.org/xml/ns/log4j-config-3.xsd">
<Appenders>
<NoSql name="MONGO">
- <MongoDb connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}/testDb.MongoDbMapMessageIT" />
+ <MongoDb
+ connection="mongodb://localhost:${sys:log4j.mongo.port:-27017}"
+ databaseName="testDb"
+ collectionName="MongoDbMapMessageIT"/>
<MessageLayout />
</NoSql>
</Appenders>
<Loggers>
<Root level="ALL">
- <AppenderRef ref="MONGO" />
+ <AppenderRef ref="MONGO"/>
</Root>
</Loggers>
</Configuration>
diff --git a/src/changelog/.3.x.x/update_org_log4j_mongodb.xml b/src/changelog/.3.x.x/update_org_log4j_mongodb.xml
new file mode 100644
index 0000000000..3b14a916b3
--- /dev/null
+++ b/src/changelog/.3.x.x/update_org_log4j_mongodb.xml
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="added">
+ <issue id="3322" link="https://github.com/apache/logging-log4j2/pull/3322"/>
+ <description format="asciidoc">Add `collectionName` and `databaseName` arguments to the MongoDB appender</description>
+</entry>
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.json b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.json
index 2b47690c0b..cac61efc98 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.json
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.json
@@ -5,7 +5,9 @@
"NoSql": {
"name": "MONGO",
"MongoDb": {
- "connection": "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs"
+ "connection": "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017",
+ "databaseName": "logging",
+ "collectionName": "logs"
},
"KeyValuePair": [
{
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.properties b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.properties
index f0b64894e2..cd12e3e642 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.properties
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.properties
@@ -19,7 +19,9 @@
Appenders.0.type = NoSql
Appenders.0.name = MONGO
Appenders.0.provider.type = MongoDB
-Appenders.0.provider.connection = mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs
+Appenders.0.provider.connection = mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017
+Appenders.0.provider.databaseName = logging
+Appenders.0.provider.collectionName = logs
Appenders.0.kv[0].type = KeyValuePair
Appenders.0.kv[0].key = startTime
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.xml b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.xml
index cd9f458ac3..60232ae5be 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.xml
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.xml
@@ -23,7 +23,7 @@
<Appenders>
<!-- tag::appender[] -->
<NoSql name="MONGO">
- <MongoDb connection="mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs"/>
+ <MongoDb connection="mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017" databaseName="logging" collectionName="logs"/>
<KeyValuePair key="startTime" value="${date:yyyy-MM-dd hh:mm:ss.SSS}"/> <!--1-->
<KeyValuePair key="currentTime" value="$${date:yyyy-MM-dd hh:mm:ss.SSS}"/> <!--2-->
</NoSql>
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.yaml b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.yaml
index 2cb5df2c85..90a8ad766d 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.yaml
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo-keys.yaml
@@ -20,7 +20,9 @@ Configuration:
NoSql:
name: "MONGO"
MongoDb:
- connection: "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs"
+ connection: "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017"
+ databaseName: "logging"
+ collectionName: "logs"
KeyValuePair:
- key: "startTime"
value: "${date:yyyy-MM-dd hh:mm:ss.SSS}" # <1>
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.json b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.json
index d06b3a190f..cf01b6a549 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.json
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.json
@@ -10,7 +10,9 @@
"NoSql": {
"name": "MONGO",
"MongoDb": {
- "connection": "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs"
+ "connection": "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017",
+ "databaseName" : "logging",
+ "collectionName": "logs"
}
}
// end::appender[]
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.properties b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.properties
index 2853c83137..c5cafac439 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.properties
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.properties
@@ -22,7 +22,9 @@ Appenders.0.layout.type = JsonTemplateLayout
Appenders.1.type = NoSql
Appenders.1.name = MONGO
Appenders.1.provider.type = MongoDB
-Appenders.1.provider.connection = mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs
+Appenders.1.provider.connection = mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017
+Appenders.1.provider.databaseName = logging
+Appenders.1.provider.collectionName = logs
# end::appender[]
# tag::loggers[]
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.xml b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.xml
index 31fe89f2f1..e05746405d 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.xml
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.xml
@@ -27,7 +27,7 @@
</File>
<!-- tag::appender[] -->
<NoSql name="MONGO">
- <MongoDb connection="mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs"/>
+ <MongoDb connection="mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017" databaseName="logging" collectionName="logs"/>
</NoSql>
<!-- end::appender[] -->
</Appenders>
diff --git a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.yaml b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.yaml
index f5bd4b6612..6469b81593 100644
--- a/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.yaml
+++ b/src/site/antora/modules/ROOT/examples/manual/appenders/database/nosql-mongo.yaml
@@ -24,7 +24,9 @@ Configuration:
NoSql:
name: "MONGO"
MongoDb:
- connection: "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017/logging.logs"
+ connection: "mongodb://${env:DB_USER}:${env:DB_PASS}@localhost:27017"
+ databaseName: "logging"
+ collectionName: "logs"
# end::appender[]
Loggers:
# tag::loggers[]
diff --git a/src/site/antora/modules/ROOT/pages/manual/appenders/database.adoc b/src/site/antora/modules/ROOT/pages/manual/appenders/database.adoc
index 079891da04..2430e9539d 100644
--- a/src/site/antora/modules/ROOT/pages/manual/appenders/database.adoc
+++ b/src/site/antora/modules/ROOT/pages/manual/appenders/database.adoc
@@ -817,6 +817,23 @@ for its format.
**Required**
+| [[MongoDbProvider-attr-databaseName]]databaseName
+| `string`
+|
+|
+It specifies the name of the database for the appender to use.
+
+Overrides the value provided in the connection string if present.
+
+| [[MongoDbProvider-attr-collectionName]]collectionName
+| `string`
+|
+|
+It specifies the name of the collection for the appender to use.
+For backward compatibility, the collection name can also be specified in the
+https://mongodb.github.io/mongo-java-driver/5.0/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html[Java-specific connection string].
+If collection name is specified in both places, the value provided here will be used.
+
| [[MongoDbProvider-attr-capped]]capped
| `boolean`
| `false`