From b9006b5cad4b4acc0570702e1752d5246a3710c4 Mon Sep 17 00:00:00 2001 From: Josh Smith Date: Sat, 21 Dec 2024 17:40:55 -0600 Subject: [PATCH 1/3] Add `collectionName` and `databaseName` attributes to `MongoDbProvider` --- log4j-mongodb/pom.xml | 5 +- .../log4j/mongodb/MongoDbConnection.java | 12 +- .../log4j/mongodb/MongoDbProvider.java | 72 +++++++++-- .../mongodb/MongoDbCollectionNameIT.java | 36 ++++++ .../MongoDbDatabaseAndCollectionNameIT.java | 36 ++++++ .../log4j/mongodb/MongoDbProviderTest.java | 112 ++++++++++++++++++ .../resources/MongoDbAdditionalFields.xml | 5 +- .../test/resources/MongoDbAuthFailureIT.xml | 4 +- .../src/test/resources/MongoDbCappedIntIT.xml | 4 +- .../test/resources/MongoDbCappedLongIT.xml | 4 +- .../resources/MongoDbCollectionNameIT.xml | 37 ++++++ .../MongoDbDatabaseAndCollectionNameIT.xml | 38 ++++++ .../src/test/resources/MongoDbIT.xml | 2 +- .../test/resources/MongoDbMapMessageIT.xml | 7 +- .../.3.x.x/update_org_log4j_mongodb.xml | 8 ++ .../appenders/database/nosql-mongo-keys.json | 4 +- .../database/nosql-mongo-keys.properties | 4 +- .../appenders/database/nosql-mongo-keys.xml | 2 +- .../appenders/database/nosql-mongo-keys.yaml | 4 +- .../appenders/database/nosql-mongo.json | 4 +- .../appenders/database/nosql-mongo.properties | 4 +- .../manual/appenders/database/nosql-mongo.xml | 2 +- .../appenders/database/nosql-mongo.yaml | 4 +- .../ROOT/pages/manual/appenders/database.adoc | 17 +++ 24 files changed, 390 insertions(+), 37 deletions(-) create mode 100644 log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCollectionNameIT.java create mode 100644 log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbDatabaseAndCollectionNameIT.java create mode 100644 log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbProviderTest.java create mode 100644 log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml create mode 100644 log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml create mode 100644 src/changelog/.3.x.x/update_org_log4j_mongodb.xml diff --git a/log4j-mongodb/pom.xml b/log4j-mongodb/pom.xml index dfd5b9529ae..70935cb3537 100644 --- a/log4j-mongodb/pom.xml +++ b/log4j-mongodb/pom.xml @@ -141,7 +141,6 @@ junit-jupiter-api test - @@ -150,9 +149,7 @@ org.apache.maven.plugins maven-surefire-plugin - - true - + 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 cabc6c73e1e..0507d8b4b41 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 @@ private static MongoCollection getOrCreateMongoCollection( } } - private final ConnectionString connectionString; private final MongoCollection 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 void insertObject(final NoSqlObject object) { @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 8bc0098809c..19feceaec24 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 @@ 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 static class Builder> extends AbstractFilterable.Bui @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 B setCollectionSize(final long collectionSize) { 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 static > B newBuilder() { 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 @@ private MongoDbProvider(final String connectionStringSource, final boolean isCap 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 00000000000..d299fddc368 --- /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 00000000000..a72c9978232 --- /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 00000000000..59346e979c3 --- /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 collection = (MongoCollection) 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 600296753eb..3af3f407236 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"> - + diff --git a/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml b/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml index 797dfb47ed7..26d9bc8e2f7 100644 --- a/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml +++ b/log4j-mongodb/src/test/resources/MongoDbAuthFailureIT.xml @@ -23,7 +23,9 @@ + connection="mongodb://log4jUser:12345678@localhost:${sys:log4j.mongo.port:-27017}" + databaseName="testDb" + collectionName="MongoDbAuthFailureIT" /> diff --git a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml b/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml index f1574d396ae..645cac894a8 100644 --- a/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml +++ b/log4j-mongodb/src/test/resources/MongoDbCappedIntIT.xml @@ -23,7 +23,9 @@ diff --git a/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml b/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml index 0ac524fdea7..16de25d2619 100644 --- a/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml +++ b/log4j-mongodb/src/test/resources/MongoDbCappedLongIT.xml @@ -24,7 +24,9 @@ diff --git a/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml b/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml new file mode 100644 index 00000000000..4e1a8511399 --- /dev/null +++ b/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml @@ -0,0 +1,37 @@ + + + + + + + + + + + + + + diff --git a/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml b/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml new file mode 100644 index 00000000000..53f81ecdebe --- /dev/null +++ b/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + diff --git a/log4j-mongodb/src/test/resources/MongoDbIT.xml b/log4j-mongodb/src/test/resources/MongoDbIT.xml index df713f01795..2ed62df6fe7 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"> - + diff --git a/log4j-mongodb/src/test/resources/MongoDbMapMessageIT.xml b/log4j-mongodb/src/test/resources/MongoDbMapMessageIT.xml index 774fa4bfaed..3e35f30fdf9 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"> - + - + 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 00000000000..3b14a916b3c --- /dev/null +++ b/src/changelog/.3.x.x/update_org_log4j_mongodb.xml @@ -0,0 +1,8 @@ + + + + Add `collectionName` and `databaseName` arguments to the MongoDB appender + 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 2b47690c0b5..cac61efc980 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 f0b64894e28..cd12e3e6424 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 cd9f458ac3a..60232ae5be6 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 @@ - + 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 2cb5df2c85f..90a8ad766d4 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 d06b3a190f6..cf01b6a549a 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 2853c83137c..c5cafac439c 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 31fe89f2f19..e05746405d6 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 @@ - + 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 f5bd4b66121..6469b81593c 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 079891da044..2430e9539dc 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` From dc996d5ca404f8f2dc9b272e7a5403d989fdf454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Sat, 31 May 2025 20:28:56 +0200 Subject: [PATCH 2/3] Fix `MongoDbProviderTest` --- .../log4j/mongodb/MongoDbProviderTest.java | 50 +++++++++---------- 1 file changed, 25 insertions(+), 25 deletions(-) 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 index 59346e979c3..b7ee3c8b5e4 100644 --- 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 @@ -16,9 +16,8 @@ */ 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 static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import com.mongodb.MongoNamespace; import com.mongodb.client.MongoCollection; @@ -42,35 +41,38 @@ void createProviderWithDatabaseAndCollectionProvidedViaConfig() { .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()); + assertThat(namespace.getCollectionName()).isEqualTo(COLL_NAME); + assertThat(namespace.getDatabaseName()).isEqualTo(DB_NAME); } @Test void createProviderWithoutDatabaseName() { - final MongoDbProvider provider = - MongoDbProvider.newBuilder().setConnectionStringSource(CS_WO_DB).build(); - assertNull(provider); + assertThatThrownBy(() -> MongoDbProvider.newBuilder() + .setConnectionStringSource(CS_WO_DB) + .build()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageStartingWith("Invalid MongoDB database name"); } @Test void createProviderWithoutDatabaseNameWithCollectionName() { - final MongoDbProvider provider = MongoDbProvider.newBuilder() - .setConnectionStringSource(CS_WO_DB) - .setCollectionName(COLL_NAME) - .build(); - assertNull(provider); + assertThatThrownBy(() -> MongoDbProvider.newBuilder() + .setConnectionStringSource(CS_WO_DB) + .setCollectionName(COLL_NAME) + .build()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageStartingWith("Invalid MongoDB database name"); } @Test void createProviderWithoutCollectionName() { - final MongoDbProvider provider = MongoDbProvider.newBuilder() - .setConnectionStringSource(CS_WO_DB) - .setDatabaseName(DB_NAME) - .build(); - assertNull(provider); + assertThatThrownBy(() -> MongoDbProvider.newBuilder() + .setConnectionStringSource(CS_WO_DB) + .setDatabaseName(DB_NAME) + .build()) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageStartingWith("Invalid MongoDB collection name"); } @Test @@ -79,10 +81,9 @@ void createProviderWithDatabaseOnConnectionString() { .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()); + assertThat(namespace.getCollectionName()).isEqualTo(COLL_NAME); + assertThat(namespace.getDatabaseName()).isEqualTo("logging"); } @Test @@ -92,10 +93,9 @@ void createProviderConfigOverridesConnectionString() { .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()); + assertThat(namespace.getCollectionName()).isEqualTo(COLL_NAME); + assertThat(namespace.getDatabaseName()).isEqualTo(DB_NAME); } private static MongoNamespace getNamespace(final MongoDbConnection connection) { From 493c830936ace01663b5c0189a37fcafc5377fb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Volkan=20Yaz=C4=B1c=C4=B1?= Date: Sat, 31 May 2025 20:32:09 +0200 Subject: [PATCH 3/3] Remove redundant tests --- .../mongodb/MongoDbCollectionNameIT.java | 36 ------------------ .../MongoDbDatabaseAndCollectionNameIT.java | 36 ------------------ .../resources/MongoDbCollectionNameIT.xml | 37 ------------------ .../MongoDbDatabaseAndCollectionNameIT.xml | 38 ------------------- 4 files changed, 147 deletions(-) delete mode 100644 log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCollectionNameIT.java delete mode 100644 log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbDatabaseAndCollectionNameIT.java delete mode 100644 log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml delete mode 100644 log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml 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 deleted file mode 100644 index d299fddc368..00000000000 --- a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCollectionNameIT.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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 deleted file mode 100644 index a72c9978232..00000000000 --- a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbDatabaseAndCollectionNameIT.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * 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/resources/MongoDbCollectionNameIT.xml b/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml deleted file mode 100644 index 4e1a8511399..00000000000 --- a/log4j-mongodb/src/test/resources/MongoDbCollectionNameIT.xml +++ /dev/null @@ -1,37 +0,0 @@ - - - - - - - - - - - - - - diff --git a/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml b/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml deleted file mode 100644 index 53f81ecdebe..00000000000 --- a/log4j-mongodb/src/test/resources/MongoDbDatabaseAndCollectionNameIT.xml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - -