diff --git a/log4j-mongodb3/.log4j-plugin-processing-activator b/log4j-mongodb/.log4j-plugin-processing-activator similarity index 100% rename from log4j-mongodb3/.log4j-plugin-processing-activator rename to log4j-mongodb/.log4j-plugin-processing-activator diff --git a/log4j-mongodb3/pom.xml b/log4j-mongodb/pom.xml similarity index 85% rename from log4j-mongodb3/pom.xml rename to log4j-mongodb/pom.xml index b1225582b2a..dd3b68facc6 100644 --- a/log4j-mongodb3/pom.xml +++ b/log4j-mongodb/pom.xml @@ -16,61 +16,49 @@ ~ limitations under the License. --> - 4.0.0 - org.apache.logging.log4j log4j ${revision} ../log4j-parent - - log4j-mongodb3 - - Apache Log4j MongoDB 3 - MongoDB appender for Log4j using the MongoDB 3 driver API. - + log4j-mongodb + Apache Log4j MongoDB Appender + MongoDB appender for Log4j using the MongoDB 5 driver API. - - - - - mongodb.driver.legacy;substitute="mongodb-driver-legacy";static=true;transitive=false - + org.apache.logging.log4j.core - - 3.12.14 + 5.0.1 + + false + true - org.mongodb bson - ${mongodb3.version} + ${mongodb5.version} org.mongodb mongodb-driver-core - ${mongodb3.version} + ${mongodb5.version} org.mongodb mongodb-driver-legacy - ${mongodb3.version} + ${mongodb5.version} org.mongodb mongodb-driver-sync - ${mongodb3.version} + ${mongodb5.version} - org.apache.logging.log4j @@ -78,7 +66,7 @@ org.apache.logging.log4j - log4j-core + log4j-mongodb4 org.mongodb @@ -88,10 +76,6 @@ org.mongodb mongodb-driver-core - - org.mongodb - mongodb-driver-legacy - org.mongodb mongodb-driver-sync @@ -144,10 +128,8 @@ test - - org.apache.maven.plugins maven-surefire-plugin @@ -163,8 +145,6 @@ - - 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 new file mode 100644 index 00000000000..0828b16648a --- /dev/null +++ b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/MongoDbProvider.java @@ -0,0 +1,46 @@ +/* + * 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 org.apache.logging.log4j.core.Core; +import org.apache.logging.log4j.core.appender.nosql.NoSqlProvider; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; +import org.apache.logging.log4j.mongodb4.MongoDb4Connection; +import org.apache.logging.log4j.mongodb4.MongoDb4Provider; +import org.apache.logging.log4j.mongodb4.MongoDb4Provider.Builder; + +/** + * Delegates to {@link MongoDb4Provider} under the name {@code MongoDb}. + */ +/* + * TODO: Currently the + * {@link org.apache.logging.log4j.core.config.plugins.processor.PluginProcessor} + * assumes that the class generated by the plugin is the same as the annotated class. + * To work around this we declare this class abstract and let it implement + * {@link NoSqlProvider}. + */ +@Plugin(name = "MongoDb", category = Core.CATEGORY_NAME, printObject = true) +public abstract class MongoDbProvider implements NoSqlProvider { + + private MongoDbProvider() {} + + @PluginBuilderFactory + public static > B newBuilder() { + return MongoDb4Provider.newBuilder(); + } +} diff --git a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/package-info.java b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/package-info.java similarity index 92% rename from log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/package-info.java rename to log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/package-info.java index 9a8fe2c1188..3ff159c9e54 100644 --- a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/package-info.java +++ b/log4j-mongodb/src/main/java/org/apache/logging/log4j/mongodb/package-info.java @@ -15,12 +15,13 @@ * limitations under the license. */ /** - * The classes in this package contain the MongoDB provider for the NoSQL Appender. + * The classes in this package contain the MongoDB provider for the NoSQL + * Appender. */ @Export @Open("org.apache.logging.log4j.core") -@Version("2.20.1") -package org.apache.logging.log4j.mongodb3; +@Version("2.24.0") +package org.apache.logging.log4j.mongodb; import aQute.bnd.annotation.jpms.Open; import org.osgi.annotation.bundle.Export; diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3CappedTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/AbstractMongoDbCappedTest.java similarity index 80% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3CappedTest.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/AbstractMongoDbCappedTest.java index 71ad196192a..07e39339941 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3CappedTest.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/AbstractMongoDbCappedTest.java @@ -14,29 +14,26 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.test.junit.LoggerContextSource; import org.bson.Document; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -@UsingMongoDb3 -@LoggerContextSource("log4j2-mongodb-capped.xml") -public class MongoDb3CappedTest { +public abstract class AbstractMongoDbCappedTest { @Test public void test(final LoggerContext ctx, final MongoClient mongoClient) { - final Logger logger = ctx.getLogger(MongoDb3CappedTest.class); + final Logger logger = ctx.getLogger(AbstractMongoDbCappedTest.class); logger.info("Hello log"); - final MongoDatabase database = mongoClient.getDatabase(MongoDb3TestConstants.DATABASE_NAME); + final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME); Assertions.assertNotNull(database); - final MongoCollection collection = database.getCollection(MongoDb3TestConstants.COLLECTION_NAME); + final MongoCollection collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME); Assertions.assertNotNull(collection); final Document first = collection.find().first(); Assertions.assertNotNull(first); diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3AdditionalFieldsTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAdditionalFieldsTest.java similarity index 91% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3AdditionalFieldsTest.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAdditionalFieldsTest.java index 3bb6f0b5f5f..c74172925f5 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3AdditionalFieldsTest.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAdditionalFieldsTest.java @@ -14,13 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.logging.log4j.Logger; @@ -29,18 +29,18 @@ import org.bson.Document; import org.junit.jupiter.api.Test; -@UsingMongoDb3 +@UsingMongoDb @LoggerContextSource("log4j2-mongodb-additional-fields.xml") -public class MongoDb3AdditionalFieldsTest { +public class MongoDbAdditionalFieldsTest { @Test public void test(final LoggerContext ctx, final MongoClient mongoClient) { - final Logger logger = ctx.getLogger(MongoDb3AdditionalFieldsTest.class); + final Logger logger = ctx.getLogger(MongoDbAdditionalFieldsTest.class); logger.info("Hello log 1"); logger.info("Hello log 2", new RuntimeException("Hello ex 2")); - final MongoDatabase database = mongoClient.getDatabase(MongoDb3TestConstants.DATABASE_NAME); + final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME); assertNotNull(database); - final MongoCollection collection = database.getCollection(MongoDb3TestConstants.COLLECTION_NAME); + final MongoCollection collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME); assertNotNull(collection); final FindIterable found = collection.find(); final Document first = found.first(); diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3AuthFailureTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthFailureTest.java similarity index 85% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3AuthFailureTest.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthFailureTest.java index 847d5c03ff5..3247dc28c06 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3AuthFailureTest.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbAuthFailureTest.java @@ -14,12 +14,12 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.logging.log4j.Logger; @@ -28,17 +28,17 @@ import org.bson.Document; import org.junit.jupiter.api.Test; -@UsingMongoDb3 +@UsingMongoDb @LoggerContextSource("log4j2-mongodb-auth-failure.xml") -public class MongoDb3AuthFailureTest { +public class MongoDbAuthFailureTest { @Test public void test(final LoggerContext ctx, final MongoClient mongoClient) { - final Logger logger = ctx.getLogger(MongoDb3AuthFailureTest.class); + final Logger logger = ctx.getLogger(MongoDbAuthFailureTest.class); logger.info("Hello log"); - final MongoDatabase database = mongoClient.getDatabase(MongoDb3TestConstants.DATABASE_NAME); + final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME); assertNotNull(database); - final MongoCollection collection = database.getCollection(MongoDb3TestConstants.DATABASE_NAME); + final MongoCollection collection = database.getCollection(MongoDbTestConstants.DATABASE_NAME); assertNotNull(collection); final Document first = collection.find().first(); assertNull(first); diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedIntTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedIntTest.java new file mode 100644 index 00000000000..500964aff08 --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedIntTest.java @@ -0,0 +1,26 @@ +/* + * 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 org.apache.logging.log4j.core.test.junit.LoggerContextSource; + +@UsingMongoDb +@LoggerContextSource("log4j2-mongodb-capped-int.xml") +public class MongoDbCappedIntTest extends AbstractMongoDbCappedTest { + + // test is in superclass +} diff --git a/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedLongTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedLongTest.java new file mode 100644 index 00000000000..34f921fe7bf --- /dev/null +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbCappedLongTest.java @@ -0,0 +1,26 @@ +/* + * 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 org.apache.logging.log4j.core.test.junit.LoggerContextSource; + +@UsingMongoDb +@LoggerContextSource("log4j2-mongodb-capped-long.xml") +public class MongoDbCappedLongTest extends AbstractMongoDbCappedTest { + + // test is in superclass +} diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3MapMessageTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTest.java similarity index 87% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3MapMessageTest.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTest.java index cada31f18bc..2733554dbd3 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3MapMessageTest.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbMapMessageTest.java @@ -14,9 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.logging.log4j.Logger; @@ -27,20 +27,20 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -@UsingMongoDb3 +@UsingMongoDb @LoggerContextSource("log4j2-mongodb-map-message.xml") -public class MongoDb3MapMessageTest { +public class MongoDbMapMessageTest { @Test public void test(final LoggerContext ctx, final MongoClient mongoClient) { - final Logger logger = ctx.getLogger(MongoDb3MapMessageTest.class); + final Logger logger = ctx.getLogger(MongoDbMapMessageTest.class); final MapMessage mapMessage = new MapMessage<>(); mapMessage.with("SomeName", "SomeValue"); mapMessage.with("SomeInt", 1); logger.info(mapMessage); - final MongoDatabase database = mongoClient.getDatabase(MongoDb3TestConstants.DATABASE_NAME); + final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME); Assertions.assertNotNull(database); - final MongoCollection collection = database.getCollection(MongoDb3TestConstants.COLLECTION_NAME); + final MongoCollection collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME); Assertions.assertNotNull(collection); final Document first = collection.find().first(); Assertions.assertNotNull(first); diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3Resolver.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbResolver.java similarity index 91% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3Resolver.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbResolver.java index 5df14b9abc4..e478ba9bbdd 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3Resolver.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbResolver.java @@ -14,9 +14,10 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; import de.flapdoodle.embed.mongo.commands.ServerAddress; import de.flapdoodle.embed.mongo.distribution.Version; import de.flapdoodle.embed.mongo.packageresolver.Command; @@ -37,6 +38,7 @@ import de.flapdoodle.reverse.TransitionWalker.ReachedState; import de.flapdoodle.reverse.transitions.Derive; import de.flapdoodle.reverse.transitions.Start; +import java.util.Objects; import java.util.function.Supplier; import org.apache.commons.lang3.NotImplementedException; import org.apache.logging.log4j.Level; @@ -52,7 +54,7 @@ import org.junit.jupiter.api.extension.ParameterContext; import org.junit.jupiter.api.extension.ParameterResolutionException; -public class MongoDb3Resolver extends TypeBasedParameterResolver implements BeforeAllCallback { +public class MongoDbResolver extends TypeBasedParameterResolver implements BeforeAllCallback { private static final Logger LOGGER = StatusLogger.getLogger(); private static final String LOGGING_TARGET_PROPERTY = "log4j2.mongoDbLoggingTarget"; @@ -75,10 +77,10 @@ private static ProcessOutput getProcessOutput(final LoggingTarget loggingTarget, default: } } - throw new NotImplementedException(loggingTarget.toString()); + throw new NotImplementedException(Objects.toString(loggingTarget)); } - public MongoDb3Resolver() { + public MongoDbResolver() { super(MongoClient.class); } @@ -142,11 +144,11 @@ private static final class MongoClientHolder implements CloseableResource, Suppl private final MongoClient mongoClient; public MongoClientHolder(final Mongod mongod, final TestProperties props) { - state = mongod.start(Version.Main.V3_6); + state = mongod.start(Version.Main.V7_0); final RunningMongodProcess mongodProcess = state.current(); final ServerAddress addr = mongodProcess.getServerAddress(); - mongoClient = new MongoClient(addr.getHost(), addr.getPort()); - props.setProperty(MongoDb3TestConstants.PROP_NAME_PORT, addr.getPort()); + mongoClient = MongoClients.create(String.format("mongodb://%s:%d", addr.getHost(), addr.getPort())); + props.setProperty(MongoDbTestConstants.PROP_NAME_PORT, addr.getPort()); } @Override @@ -169,13 +171,17 @@ public StatusLoggerStreamProcessor(Level level) { this.level = level; } + @Override public void process(String line) { LOGGER.log(level, () -> stripLineEndings(line)); } - public void onProcessed() {} + @Override + public void onProcessed() { + // noop + } - protected String stripLineEndings(String line) { + String stripLineEndings(String line) { // we still need to remove line endings that are passed on by // StreamToLineProcessor... return line.replaceAll("[\n\r]+", ""); diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3ResolverTest.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbResolverTest.java similarity index 90% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3ResolverTest.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbResolverTest.java index 46c7b2eb16a..199cabcb2fe 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3ResolverTest.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbResolverTest.java @@ -14,11 +14,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mongodb.MongoClient; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoIterable; import org.junit.jupiter.api.Test; @@ -28,8 +28,8 @@ * The test framework {@code de.flapdoodle.embed.mongo} requires Java 8. *

*/ -@UsingMongoDb3 -public class MongoDb3ResolverTest { +@UsingMongoDb +public class MongoDbResolverTest { @Test public void testAccess(final MongoClient mongoClient) { diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3Test.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java similarity index 89% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3Test.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java index 449c0819bf7..5c4bbb750c1 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3Test.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTest.java @@ -14,13 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import com.mongodb.MongoClient; import com.mongodb.client.FindIterable; +import com.mongodb.client.MongoClient; import com.mongodb.client.MongoCollection; import com.mongodb.client.MongoDatabase; import org.apache.logging.log4j.Logger; @@ -29,18 +29,18 @@ import org.bson.Document; import org.junit.jupiter.api.Test; -@UsingMongoDb3 +@UsingMongoDb @LoggerContextSource("log4j2-mongodb.xml") -public class MongoDb3Test { +public class MongoDbTest { @Test public void test(final LoggerContext ctx, final MongoClient mongoClient) { - final Logger logger = ctx.getLogger(MongoDb3Test.class); + final Logger logger = ctx.getLogger(MongoDbTest.class); logger.info("Hello log 1"); logger.info("Hello log 2", new RuntimeException("Hello ex 2")); - final MongoDatabase database = mongoClient.getDatabase(MongoDb3TestConstants.DATABASE_NAME); + final MongoDatabase database = mongoClient.getDatabase(MongoDbTestConstants.DATABASE_NAME); assertNotNull(database); - final MongoCollection collection = database.getCollection(MongoDb3TestConstants.COLLECTION_NAME); + final MongoCollection collection = database.getCollection(MongoDbTestConstants.COLLECTION_NAME); assertNotNull(collection); final FindIterable found = collection.find(); final Document first = found.first(); diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3TestConstants.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestConstants.java similarity index 92% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3TestConstants.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestConstants.java index 1b06fce9f3d..ed5435599b9 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/MongoDb3TestConstants.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/MongoDbTestConstants.java @@ -14,9 +14,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; -public class MongoDb3TestConstants { +public class MongoDbTestConstants { public static final String PROP_NAME_PORT = "MongoDBTestPort"; static final String COLLECTION_NAME = "testCollection"; diff --git a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/UsingMongoDb3.java b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/UsingMongoDb.java similarity index 92% rename from log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/UsingMongoDb3.java rename to log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/UsingMongoDb.java index 47a5c89f18c..8ad70c0d203 100644 --- a/log4j-mongodb3/src/test/java/org/apache/logging/log4j/mongodb3/UsingMongoDb3.java +++ b/log4j-mongodb/src/test/java/org/apache/logging/log4j/mongodb/UsingMongoDb.java @@ -14,7 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.apache.logging.log4j.mongodb3; +package org.apache.logging.log4j.mongodb; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; @@ -32,5 +32,5 @@ @Inherited @Documented @ExtendWith(ExtensionContextAnchor.class) -@ExtendWith(MongoDb3Resolver.class) -public @interface UsingMongoDb3 {} +@ExtendWith(MongoDbResolver.class) +public @interface UsingMongoDb {} diff --git a/log4j-mongodb3/src/test/resources/log4j2-mongodb-additional-fields.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb-additional-fields.xml similarity index 88% rename from log4j-mongodb3/src/test/resources/log4j2-mongodb-additional-fields.xml rename to log4j-mongodb/src/test/resources/log4j2-mongodb-additional-fields.xml index e75b9b6f8ee..d9d27461a00 100644 --- a/log4j-mongodb3/src/test/resources/log4j2-mongodb-additional-fields.xml +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb-additional-fields.xml @@ -15,11 +15,10 @@ ~ See the License for the specific language governing permissions and ~ limitations under the License. --> - + - + diff --git a/log4j-mongodb3/src/test/resources/log4j2-mongodb-auth-failure.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb-auth-failure.xml similarity index 85% rename from log4j-mongodb3/src/test/resources/log4j2-mongodb-auth-failure.xml rename to log4j-mongodb/src/test/resources/log4j2-mongodb-auth-failure.xml index 434c70ceea6..e84603a0a52 100644 --- a/log4j-mongodb3/src/test/resources/log4j2-mongodb-auth-failure.xml +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb-auth-failure.xml @@ -18,8 +18,8 @@ - + diff --git a/log4j-mongodb3/src/test/resources/log4j2-mongodb-capped.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb-capped-int.xml similarity index 85% rename from log4j-mongodb3/src/test/resources/log4j2-mongodb-capped.xml rename to log4j-mongodb/src/test/resources/log4j2-mongodb-capped-int.xml index b147e8cd5fd..746b19914e7 100644 --- a/log4j-mongodb3/src/test/resources/log4j2-mongodb-capped.xml +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb-capped-int.xml @@ -18,8 +18,10 @@ - + diff --git a/log4j-mongodb/src/test/resources/log4j2-mongodb-capped-long.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb-capped-long.xml new file mode 100644 index 00000000000..941ea71c4e2 --- /dev/null +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb-capped-long.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + diff --git a/log4j-mongodb3/src/test/resources/log4j2-mongodb-map-message.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml similarity index 88% rename from log4j-mongodb3/src/test/resources/log4j2-mongodb-map-message.xml rename to log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml index 16fd4a5d4de..4b740bc947e 100644 --- a/log4j-mongodb3/src/test/resources/log4j2-mongodb-map-message.xml +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb-map-message.xml @@ -18,8 +18,7 @@ - + diff --git a/log4j-mongodb3/src/test/resources/log4j2-mongodb.xml b/log4j-mongodb/src/test/resources/log4j2-mongodb.xml similarity index 88% rename from log4j-mongodb3/src/test/resources/log4j2-mongodb.xml rename to log4j-mongodb/src/test/resources/log4j2-mongodb.xml index d00d48124a4..4f0865b2c46 100644 --- a/log4j-mongodb3/src/test/resources/log4j2-mongodb.xml +++ b/log4j-mongodb/src/test/resources/log4j2-mongodb.xml @@ -18,8 +18,7 @@ - + diff --git a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/LevelCodec.java b/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/LevelCodec.java deleted file mode 100644 index 5e6826d4580..00000000000 --- a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/LevelCodec.java +++ /dev/null @@ -1,52 +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.mongodb3; - -import org.apache.logging.log4j.Level; -import org.bson.BsonReader; -import org.bson.BsonWriter; -import org.bson.codecs.Codec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.EncoderContext; - -/** - * A BSON Codec for Log4j {@link Level}s. - */ -public class LevelCodec implements Codec { - - /** - * The singleton instance. - * - * @since 2.14.0 - */ - public static final LevelCodec INSTANCE = new LevelCodec(); - - @Override - public Level decode(final BsonReader reader, final DecoderContext decoderContext) { - return Level.getLevel(reader.readString()); - } - - @Override - public void encode(final BsonWriter writer, final Level level, final EncoderContext encoderContext) { - writer.writeString(level.name()); - } - - @Override - public Class getEncoderClass() { - return Level.class; - } -} diff --git a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbConnection.java b/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbConnection.java deleted file mode 100644 index ac34650ee49..00000000000 --- a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbConnection.java +++ /dev/null @@ -1,102 +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.mongodb3; - -import com.mongodb.MongoClient; -import com.mongodb.MongoException; -import com.mongodb.client.MongoCollection; -import com.mongodb.client.MongoDatabase; -import com.mongodb.client.model.CreateCollectionOptions; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.core.appender.AppenderLoggingException; -import org.apache.logging.log4j.core.appender.nosql.AbstractNoSqlConnection; -import org.apache.logging.log4j.core.appender.nosql.NoSqlConnection; -import org.apache.logging.log4j.core.appender.nosql.NoSqlObject; -import org.apache.logging.log4j.status.StatusLogger; -import org.bson.Document; - -/** - * The MongoDB implementation of {@link NoSqlConnection}. - */ -public final class MongoDbConnection extends AbstractNoSqlConnection { - - private static final Logger LOGGER = StatusLogger.getLogger(); - - private static MongoCollection getOrCreateMongoCollection( - final MongoDatabase database, - final String collectionName, - final boolean isCapped, - final Integer sizeInBytes) { - try { - LOGGER.debug("Gettting collection '{}'...", collectionName); - // throws IllegalArgumentException if collectionName is invalid - return database.getCollection(collectionName); - } catch (final IllegalStateException e) { - LOGGER.debug("Collection '{}' does not exist.", collectionName); - final CreateCollectionOptions options = new CreateCollectionOptions() - // @formatter:off - .capped(isCapped) - .sizeInBytes(sizeInBytes); - // @formatter:on - LOGGER.debug( - "Creating collection {} (capped = {}, sizeInBytes = {})", collectionName, isCapped, sizeInBytes); - database.createCollection(collectionName, options); - return database.getCollection(collectionName); - } - } - - private final MongoCollection collection; - private final MongoClient mongoClient; - - public MongoDbConnection( - final MongoClient mongoClient, - final MongoDatabase mongoDatabase, - final String collectionName, - final boolean isCapped, - final Integer sizeInBytes) { - this.mongoClient = mongoClient; - this.collection = getOrCreateMongoCollection(mongoDatabase, collectionName, isCapped, sizeInBytes); - } - - @Override - public void closeImpl() { - // LOG4J2-1196 - mongoClient.close(); - } - - @Override - public MongoDbDocumentObject[] createList(final int length) { - return new MongoDbDocumentObject[length]; - } - - @Override - public MongoDbDocumentObject createObject() { - return new MongoDbDocumentObject(); - } - - @Override - public void insertObject(final NoSqlObject object) { - try { - final Document unwrapped = object.unwrap(); - LOGGER.debug("Inserting object {}", unwrapped); - this.collection.insertOne(unwrapped); - } catch (final MongoException e) { - throw new AppenderLoggingException( - "Failed to write log event to MongoDB due to error: " + e.getMessage(), e); - } - } -} diff --git a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbDocumentObject.java b/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbDocumentObject.java deleted file mode 100644 index 31798bc2db6..00000000000 --- a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbDocumentObject.java +++ /dev/null @@ -1,61 +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.mongodb3; - -import java.util.Arrays; -import org.apache.logging.log4j.core.appender.nosql.NoSqlObject; -import org.bson.Document; - -/** - * The MongoDB implementation of {@link NoSqlObject} typed to a BSON {@link Document}. - */ -public final class MongoDbDocumentObject implements NoSqlObject { - - private final Document document; - - /** - * Constructs a new instance. - */ - public MongoDbDocumentObject() { - this.document = new Document(); - } - - @Override - public void set(final String field, final NoSqlObject value) { - this.document.append(field, value != null ? value.unwrap() : null); - } - - @Override - public void set(final String field, final NoSqlObject[] values) { - this.document.append(field, values != null ? Arrays.asList(values) : null); - } - - @Override - public void set(final String field, final Object value) { - this.document.append(field, value); - } - - @Override - public void set(final String field, final Object[] values) { - this.document.append(field, values != null ? Arrays.asList(values) : null); - } - - @Override - public Document unwrap() { - return this.document; - } -} diff --git a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbDocumentObjectCodec.java b/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbDocumentObjectCodec.java deleted file mode 100644 index 812ca96ee38..00000000000 --- a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbDocumentObjectCodec.java +++ /dev/null @@ -1,49 +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.mongodb3; - -import org.bson.BsonReader; -import org.bson.BsonWriter; -import org.bson.Document; -import org.bson.codecs.Codec; -import org.bson.codecs.DecoderContext; -import org.bson.codecs.DocumentCodec; -import org.bson.codecs.EncoderContext; - -final class MongoDbDocumentObjectCodec implements Codec { - - private final Codec documentCodec = new DocumentCodec(); - - @Override - public void encode( - final BsonWriter writer, final MongoDbDocumentObject value, final EncoderContext encoderContext) { - documentCodec.encode(writer, value.unwrap(), encoderContext); - } - - @Override - public Class getEncoderClass() { - return MongoDbDocumentObject.class; - } - - @Override - public MongoDbDocumentObject decode(final BsonReader reader, final DecoderContext decoderContext) { - final MongoDbDocumentObject object = new MongoDbDocumentObject(); - documentCodec.decode(reader, decoderContext).entrySet().stream() - .forEach(entry -> object.set(entry.getKey(), entry.getValue())); - return object; - } -} diff --git a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbProvider.java b/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbProvider.java deleted file mode 100644 index 90a9a6f4928..00000000000 --- a/log4j-mongodb3/src/main/java/org/apache/logging/log4j/mongodb3/MongoDbProvider.java +++ /dev/null @@ -1,352 +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.mongodb3; - -import com.mongodb.MongoClient; -import com.mongodb.MongoClientOptions; -import com.mongodb.MongoCredential; -import com.mongodb.ServerAddress; -import com.mongodb.WriteConcern; -import com.mongodb.client.MongoDatabase; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.core.Core; -import org.apache.logging.log4j.core.appender.nosql.NoSqlProvider; -import org.apache.logging.log4j.core.config.plugins.Plugin; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute; -import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory; -import org.apache.logging.log4j.core.config.plugins.convert.TypeConverters; -import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required; -import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidHost; -import org.apache.logging.log4j.core.config.plugins.validation.constraints.ValidPort; -import org.apache.logging.log4j.core.filter.AbstractFilterable; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.LoaderUtil; -import org.apache.logging.log4j.util.Strings; -import org.bson.codecs.configuration.CodecRegistries; -import org.bson.codecs.configuration.CodecRegistry; - -/** - * The MongoDB implementation of {@link NoSqlProvider}.using the MongoDB driver version 3 API. - */ -@Plugin(name = "MongoDb3", category = Core.CATEGORY_NAME, printObject = true) -public final class MongoDbProvider implements NoSqlProvider { - - public static class Builder> extends AbstractFilterable.Builder - implements org.apache.logging.log4j.core.util.Builder { - - // @formatter:off - private static final CodecRegistry CODEC_REGISTRIES = CodecRegistries.fromRegistries( - MongoClient.getDefaultCodecRegistry(), - CodecRegistries.fromCodecs(LevelCodec.INSTANCE), - CodecRegistries.fromCodecs(new MongoDbDocumentObjectCodec())); - // @formatter:on - - private static WriteConcern toWriteConcern( - final String writeConcernConstant, final String writeConcernConstantClassName) { - WriteConcern writeConcern; - if (Strings.isNotEmpty(writeConcernConstant)) { - if (Strings.isNotEmpty(writeConcernConstantClassName)) { - try { - final Class writeConcernConstantClass = LoaderUtil.loadClass(writeConcernConstantClassName); - final Field field = writeConcernConstantClass.getField(writeConcernConstant); - writeConcern = (WriteConcern) field.get(null); - } catch (final Exception e) { - LOGGER.error( - "Write concern constant [{}.{}] not found, using default.", - writeConcernConstantClassName, - writeConcernConstant); - writeConcern = DEFAULT_WRITE_CONCERN; - } - } else { - writeConcern = WriteConcern.valueOf(writeConcernConstant); - if (writeConcern == null) { - LOGGER.warn("Write concern constant [{}] not found, using default.", writeConcernConstant); - writeConcern = DEFAULT_WRITE_CONCERN; - } - } - } else { - writeConcern = DEFAULT_WRITE_CONCERN; - } - return writeConcern; - } - - @PluginBuilderAttribute - @Required(message = "No collection name provided") - private String collectionName; - - @PluginBuilderAttribute - private int collectionSize = DEFAULT_COLLECTION_SIZE; - - @PluginBuilderAttribute - @Required(message = "No database name provided") - private String databaseName; - - @PluginBuilderAttribute - private String factoryClassName; - - @PluginBuilderAttribute - private String factoryMethodName; - - @PluginBuilderAttribute("capped") - private boolean capped = false; - - @PluginBuilderAttribute(sensitive = true) - private String password; - - @PluginBuilderAttribute - @ValidPort - private String port = "" + DEFAULT_PORT; - - @PluginBuilderAttribute - @ValidHost - private String server = "localhost"; - - @PluginBuilderAttribute - private String userName; - - @PluginBuilderAttribute - private String writeConcernConstant; - - @PluginBuilderAttribute - private String writeConcernConstantClassName; - - @SuppressWarnings("resource") - @Override - public MongoDbProvider build() { - MongoDatabase database; - String description; - MongoClient mongoClient = null; - - 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 MongoDatabase) { - database = (MongoDatabase) object; - } else if (object instanceof MongoClient) { - if (Strings.isNotEmpty(databaseName)) { - database = ((MongoClient) object).getDatabase(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); - } else { - LOGGER.error( - "The factory method [{}.{}()] returned an unsupported type [{}].", - factoryClassName, - factoryMethodName, - object.getClass().getName()); - } - return null; - } - - final String databaseName = database.getName(); - description = "database=" + databaseName; - } 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)) { - MongoCredential mongoCredential = null; - description = "database=" + databaseName; - if (Strings.isNotEmpty(userName) && Strings.isNotEmpty(password)) { - description += ", username=" + userName; - mongoCredential = MongoCredential.createCredential(userName, databaseName, password.toCharArray()); - } - try { - final int portInt = TypeConverters.convert(port, int.class, DEFAULT_PORT); - description += ", server=" + server + ", port=" + portInt; - final WriteConcern writeConcern = - toWriteConcern(writeConcernConstant, writeConcernConstantClassName); - // @formatter:off - final MongoClientOptions options = MongoClientOptions.builder() - .codecRegistry(CODEC_REGISTRIES) - .writeConcern(writeConcern) - .build(); - // @formatter:on - final ServerAddress serverAddress = new ServerAddress(server, portInt); - mongoClient = mongoCredential == null - ? - // @formatter:off - new MongoClient(serverAddress, options) - : new MongoClient(serverAddress, mongoCredential, options); - // @formatter:on - database = mongoClient.getDatabase(databaseName); - } catch (final Exception e) { - LOGGER.error( - "Failed to obtain a database instance from the MongoClient at server [{}] and " - + "port [{}].", - server, - port); - close(mongoClient); - return null; - } - } else { - LOGGER.error("No factory method was provided so the database name is required."); - close(mongoClient); - return null; - } - - try { - database.listCollectionNames().first(); // 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); - close(mongoClient); - return null; - } - - return new MongoDbProvider(mongoClient, database, collectionName, capped, collectionSize, description); - } - - private void close(final MongoClient mongoClient) { - if (mongoClient != null) { - mongoClient.close(); - } - } - - public B setCapped(final boolean isCapped) { - this.capped = isCapped; - return asBuilder(); - } - - public B setCollectionName(final String collectionName) { - this.collectionName = collectionName; - return asBuilder(); - } - - public B setCollectionSize(final int collectionSize) { - this.collectionSize = collectionSize; - return asBuilder(); - } - - public B setDatabaseName(final String databaseName) { - this.databaseName = databaseName; - return asBuilder(); - } - - public B setFactoryClassName(final String factoryClassName) { - this.factoryClassName = factoryClassName; - return asBuilder(); - } - - public B setFactoryMethodName(final String factoryMethodName) { - this.factoryMethodName = factoryMethodName; - return asBuilder(); - } - - public B setPassword(final String password) { - this.password = password; - return asBuilder(); - } - - public B setPort(final String port) { - this.port = port; - return asBuilder(); - } - - public B setServer(final String server) { - this.server = server; - return asBuilder(); - } - - public B setUserName(final String userName) { - this.userName = userName; - return asBuilder(); - } - - public B setWriteConcernConstant(final String writeConcernConstant) { - this.writeConcernConstant = writeConcernConstant; - return asBuilder(); - } - - public B setWriteConcernConstantClassName(final String writeConcernConstantClassName) { - this.writeConcernConstantClassName = writeConcernConstantClassName; - return asBuilder(); - } - } - - private static final int DEFAULT_COLLECTION_SIZE = 536870912; - private static final int DEFAULT_PORT = 27017; - private static final WriteConcern DEFAULT_WRITE_CONCERN = WriteConcern.ACKNOWLEDGED; - - private static final Logger LOGGER = StatusLogger.getLogger(); - - @PluginBuilderFactory - public static > B newBuilder() { - return new Builder().asBuilder(); - } - - private final String collectionName; - private final Integer collectionSize; - private final String description; - private final boolean isCapped; - private final MongoClient mongoClient; - private final MongoDatabase mongoDatabase; - - private MongoDbProvider( - final MongoClient mongoClient, - final MongoDatabase mongoDatabase, - final String collectionName, - final boolean isCapped, - final Integer collectionSize, - final String description) { - this.mongoClient = mongoClient; - this.mongoDatabase = mongoDatabase; - this.collectionName = collectionName; - this.isCapped = isCapped; - this.collectionSize = collectionSize; - this.description = "mongoDb{ " + description + " }"; - } - - @Override - public MongoDbConnection getConnection() { - return new MongoDbConnection(mongoClient, mongoDatabase, collectionName, isCapped, collectionSize); - } - - @Override - public String toString() { - return description; - } -} diff --git a/log4j-mongodb4/src/main/java/org/apache/logging/log4j/mongodb4/MongoDb4Provider.java b/log4j-mongodb4/src/main/java/org/apache/logging/log4j/mongodb4/MongoDb4Provider.java index 171b4d296c7..299e369630b 100644 --- a/log4j-mongodb4/src/main/java/org/apache/logging/log4j/mongodb4/MongoDb4Provider.java +++ b/log4j-mongodb4/src/main/java/org/apache/logging/log4j/mongodb4/MongoDb4Provider.java @@ -37,9 +37,11 @@ * The MongoDB implementation of {@link NoSqlProvider} using the MongoDB driver * version 4 API. */ -@Plugin(name = "MongoDb4", category = Core.CATEGORY_NAME, printObject = true) +@Plugin(name = MongoDb4Provider.PLUGIN_NAME, category = Core.CATEGORY_NAME, printObject = true) public final class MongoDb4Provider implements NoSqlProvider { + static final String PLUGIN_NAME = "MongoDb4"; + public static class Builder> extends AbstractFilterable.Builder implements org.apache.logging.log4j.core.util.Builder { @@ -55,6 +57,7 @@ public static class Builder> extends AbstractFilterable.Bui @Override public MongoDb4Provider build() { + StatusLogger.getLogger().warn("The {} Appender is deprecated, use the MongoDb Appender.", PLUGIN_NAME); return new MongoDb4Provider(connectionStringSource, capped, collectionSize); } diff --git a/log4j-mongodb4/src/test/java/org/apache/logging/log4j/mongodb4/MongoDb4Resolver.java b/log4j-mongodb4/src/test/java/org/apache/logging/log4j/mongodb4/MongoDb4Resolver.java index fe4a4c18a29..da937eb3ec9 100644 --- a/log4j-mongodb4/src/test/java/org/apache/logging/log4j/mongodb4/MongoDb4Resolver.java +++ b/log4j-mongodb4/src/test/java/org/apache/logging/log4j/mongodb4/MongoDb4Resolver.java @@ -146,7 +146,7 @@ private static final class MongoClientHolder implements CloseableResource, Suppl private final MongoClient mongoClient; public MongoClientHolder(final Mongod mongod, final TestProperties props) { - state = mongod.start(Version.Main.V4_4); + state = mongod.start(Version.Main.V7_0); final RunningMongodProcess mongodProcess = state.current(); final ServerAddress addr = mongodProcess.getServerAddress(); mongoClient = MongoClients.create(String.format("mongodb://%s:%d", addr.getHost(), addr.getPort())); diff --git a/pom.xml b/pom.xml index 5d5459ee91a..a1883a55dca 100644 --- a/pom.xml +++ b/pom.xml @@ -257,7 +257,7 @@ log4j-jul log4j-layout-template-json log4j-layout-template-json-test - log4j-mongodb3 + log4j-mongodb log4j-mongodb4 log4j-osgi-test log4j-perf-test @@ -473,13 +473,13 @@ org.apache.logging.log4j - log4j-mongodb3 + log4j-mongodb4 ${project.version} org.apache.logging.log4j - log4j-mongodb4 + log4j-mongodb ${project.version} diff --git a/src/changelog/.2.x.x/add_log4j_mongodb.xml b/src/changelog/.2.x.x/add_log4j_mongodb.xml new file mode 100644 index 00000000000..8227ff3d54b --- /dev/null +++ b/src/changelog/.2.x.x/add_log4j_mongodb.xml @@ -0,0 +1,7 @@ + + + Add module log4j-mongodb to track the current MongoDB driver (currently version 5). + diff --git a/src/changelog/.2.x.x/deprecate_log4j_mongodb4.xml b/src/changelog/.2.x.x/deprecate_log4j_mongodb4.xml new file mode 100644 index 00000000000..e83cd2e7f14 --- /dev/null +++ b/src/changelog/.2.x.x/deprecate_log4j_mongodb4.xml @@ -0,0 +1,7 @@ + + + Deprecate the log4j-mongodb4 module in favor of log4j-mongodb. + diff --git a/src/changelog/.2.x.x/remove_log4j_mongodb3.xml b/src/changelog/.2.x.x/remove_log4j_mongodb3.xml new file mode 100644 index 00000000000..310e97f51be --- /dev/null +++ b/src/changelog/.2.x.x/remove_log4j_mongodb3.xml @@ -0,0 +1,7 @@ + + + Remove module log4j-mongodb3, use log4j-mongodb instead, log4j-mongodb4 is deprecated for removal. + diff --git a/src/site/antora/modules/ROOT/pages/manual/appenders.adoc b/src/site/antora/modules/ROOT/pages/manual/appenders.adoc index 980dac7c529..f473f73483e 100644 --- a/src/site/antora/modules/ROOT/pages/manual/appenders.adoc +++ b/src/site/antora/modules/ROOT/pages/manual/appenders.adoc @@ -1919,96 +1919,61 @@ databases if represented in a JSON format: } ---- -[#NoSQLAppenderMongoDB] -== NoSQLAppenderMongoDB +[#NoSQLAppenderMongoDBMain] +== NoSQL Appenders for MongoDB Starting with Log4 2.11.0, we provide the following MongoDB modules: -* Added in v2.11.0, dropped in v2.14.0: `log4j-mongodb2` defines the configuration element MongoDb2 matching the MongoDB Driver version 2. -* Added in v2.11.0: `log4j-mongodb3` defines the configuration element MongoDb3 matching the MongoDB Driver version 3. -* Added in 2.14.0: `log4j-mongodb4` defines the configuration element -link:#NoSQLAppenderMongoDB4[`MongoDb4`] matching the MongoDB Driver -version 4. +* Added in 2.11.0, removed in 2.14.0: `log4j-mongodb2` defines the configuration element MongoDb2 matching the MongoDB Driver version 2. +* Added in 2.11.0, removed in 2.24.0: `log4j-mongodb3` defines the configuration element MongoDb3 matching the MongoDB Driver version 3. +* Added in 2.14.0, deprecated in 2.24.0: `log4j-mongodb4` defines the configuration element +link:#NoSQLAppenderMongoDB4[`MongoDb4`] matching the MongoDB Driver version 4. +* Added in 2.24.0: `log4j-mongodb` defines the configuration element +link:#NoSQLAppenderMongoDBCurrent[`MongoDb`] matching the current MongoDB Driver (version 5). This module tracks the current MongoDB Driver. -We no longer provide the modules `log4j-mongodb`. +[#NoSQLAppenderMongoDBCurrent] +== NoSQL Appender for MongoDB -The module `log4j-mongodb2` aliases the old configuration element `MongoDb` to `MongoDb2`. - -[[NoSQLAppenderMongoDB3]] -== NoSQLAppender for MongoDB 3 - -This section details specializations of the link:#NoSQLAppender[NoSQLAppender] provider for MongoDB using the MongoDB driver version 3. The NoSQLAppender Appender writes log events to a NoSQL database using an internal lightweight provider interface. +This section details specializations of the +link:#NoSQLAppender[NoSQLAppender] provider for MongoDB using the +current MongoDB driver (version 5). The NoSQLAppender Appender writes log events +to a NoSQL database using an internal lightweight provider interface. -[width="100%",options="header"] -|=== +.MongoDB Provider Parameters +[cols="20%,20%,60%",options="header",] +|======================================================================= |Parameter Name |Type |Description +|connection |String |_Required._ The MongoDB +http://mongodb.github.io/mongo-java-driver/5.0/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html?is-external=true"[connection string] +in the format `mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database.collection][?options]]`. -|collectionName -|String -|_Required._ The name of the MongoDB collection to insert the events into. - -|writeConcernConstant -|Field -|By default, the MongoDB provider inserts records with the instructions `com.mongodb.WriteConcern.ACKNOWLEDGED`. Use this optional attribute to specify the name of a constant other than `ACKNOWLEDGED`. - -|writeConcernConstantClass -|Class -|If you specify `writeConcernConstant`, you can use this attribute to specify a class other than `com.mongodb.WriteConcern` to find the constant on (to create your own custom instructions). - -|factoryClassName -|Class -|To provide a connection to the MongoDB database, you can use this attribute and `factoryMethodName` to specify a class and static method to get the connection from. The method must return a `com.mongodb.client.MongoDatabase` or a `com.mongodb.MongoClient`. If the `com.mongodb.client.MongoDatabase` is not authenticated, you must also specify a `username` and `password`. If you use the factory method for providing a connection, you must not specify the `databaseName`, `server`, or `port` attributes. - -|factoryMethodName -|Method -|See the documentation for attribute `factoryClassName`. - -|databaseName -|String -|If you do not specify a `factoryClassName` and `factoryMethodName` for providing a MongoDB connection, you must specify a MongoDB database name using this attribute. You must also specify a `username` and `password`. You can optionally also specify a `server` (defaults to localhost), and a `port` (defaults to the default MongoDB port). - -|server -|String -|See the documentation for attribute `databaseName`. - -|port -|int -|See the documentation for attribute `databaseName`. - -|username -|String -|See the documentation for attributes `databaseName` and `factoryClassName`. - -|password -|String -|See the documentation for attributes `databaseName` and `factoryClassName`. - -|capped -|boolean -|Enable support for https://docs.mongodb.com/manual/core/capped-collections/[capped collections]. +|capped |boolean |Enable support for +https://docs.mongodb.com/manual/core/capped-collections/[capped +collections] -|collectionSize -|int -|Specify the size in bytes of the capped collection to use if enabled. The minimum size is 4096 bytes, and larger sizes will be increased to the nearest integer multiple of 256. See the capped collection documentation linked above for more information. -|=== +|collectionSize |long |Specify the size in bytes of the capped collection +to use if enabled. The minimum size is 4096 bytes, and larger sizes will +be increased to the nearest integer multiple of 256. See the capped +collection documentation linked above for more information. +|======================================================================= This appender is xref:manual/messages.adoc#MapMessage[MapMessage]-aware. -Here are a few sample configurations for the NoSQLAppender and MongoDB3 provider: +Here are a few sample configurations for the NoSQLAppender and MongoDB4 +provider: [source,xml] ---- - + - - + + - - + + @@ -2017,47 +1982,50 @@ Here are a few sample configurations for the NoSQLAppender and MongoDB3 provider [source,xml] ---- - + - - + + - - + + ---- -You can define additional fields to log using `KeyValuePair` elements, for example: +You can define additional fields to log using KeyValuePair elements, for example: [source,xml] ---- - - - - - - - - - - - - - - + + + + + + + + + + + + + + ---- [#NoSQLAppenderMongoDB4] -== NoSQLAppenderMongoDB4 +== (Deprecated) No SQLAppender for MongoDB 4 + +The `log4j-mongodb4` module is deprecated in favor of link:#NoSQLAppenderMongoDBCurrent[NoSQLAppender for MongoDB]. This section details specializations of the link:#NoSQLAppender[NoSQLAppender] provider for MongoDB using the diff --git a/src/site/antora/modules/ROOT/pages/manual/messages.adoc b/src/site/antora/modules/ROOT/pages/manual/messages.adoc index 7769192eb81..8f2128f4303 100644 --- a/src/site/antora/modules/ROOT/pages/manual/messages.adoc +++ b/src/site/antora/modules/ROOT/pages/manual/messages.adoc @@ -225,7 +225,7 @@ with a no layout, it converts a Log4j `MapMessage` to a JMS * When a xref:manual/appenders.adoc#JDBCAppender[JDBC Appender] is configured with no layout, it converts a Log4j `MapMessage` to values in a SQL INSERT statement. -* When a xref:manual/appenders.adoc#NoSQLAppenderMongoDB4[MongoDB4 Appender] is +* When a xref:manual/appenders.adoc#NoSQLAppenderMongoDBMain[MongoDB Appender] is configured with no layout, it converts a Log4j `MapMessage` to fields in a MongoDB object.