简体   繁体   中英

How to enable logging in the MongoDB Java-Driver

I have been using MongoDB 4.2 recently in combination with java-driver 3.12.7. After I created a new MongoDB project with the Version (MongoDB) 4.4 i also had to use the newest java-driver (for example java-driver 4.1 or 4.2). (Btw I don't use maven or grandle). Since I got the java-driver sync 4.2 I tried to connect to my database with my java app. The app worked fine with the combination MongoDB 4.2 & java-driver 3.12.7.

Unfortunately, I constantly receive the notification:

com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J WARNING: SLF4J not found on the classpath. Logging is disabled for the 'org.mongodb.driver' component

I looked up on MongoDB and found this:

By default, logging is enabled via the popular SLF4J API. Logging is optional: the driver will use SLF4J if the driver detects the presence of SLF4J API (class org.slf4j.Logger) in the classpath. Otherwise, the driver will log a single warning via JUL (java.util.logging) and otherwise logging will be disabled.

Are there any solutions to solve the problem with SLF4J without using SLF4J? I want to see the logs like before with JUL. At the moment I'm using IntelliJ as my IDE and OpenJDK15.0.02.

After i updated MongoDB & the java-driver nothing worked for some reason. I took this code as an example to show the problem. I put two System.out.println() in it where i get the Point1 & Point2 as expected. But however i cant get a connection to the database?:

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import java.util.Arrays;

       

public class TestClass {
   public static void main(String[] args) {
         System.out.println("hello1");
         MongoClient mongoClient = MongoClients.create("my URI stands here");
         MongoDatabase database = mongoClient.getDatabase("DatabaseMongo");
         MongoCollection<Document> collection = database.getCollection("test");
         System.out.println("hello2");
         Document doc = new Document("name", "MongoDB")
                .append("type", "database")
                .append("count", 1)
                .append("versions", Arrays.asList("v3.2", "v3.0", "v2.6"))
                .append("info", new Document("x", 203).append("y", 102));
         collection.insertOne(doc);
    }

}

After the compiling i get this:

  1. Point1

  2. 15, 2021 1:14:43 AM com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J

  3. WARNING: SLF4J not found on the classpath. Logging is disabled for the 'org.mongodb.driver' component

  4. Point2

  5. Exception in thread "main" com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@5a4041cc. Client view of cluster state is {type=REPLICA_SET, servers=[{address=testcluster-shard-00-00.3lmlx.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=testcluster-shard-00-02.3lmlx.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}, {address=testcluster-shard-00-01.3lmlx.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]

  6. here are some other reports which are maybe not important..

  7. at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:453)

  8. at com.mongodb.client.internal.MongoCollectionImpl.insertOne(MongoCollectionImpl.java:447)

  9. at TestClass.main(TestClass.java:27) //it is the line with insertOne

  10. Process finished with exit code 1

The solution to the second problem: MongoDB Atlas-> connection -> add new database user and delete the "old" one. After this it works.

This is expected behavior with 4.x Java driver releases. The change is documented here :

The driver no longer logs via JUL (java.util.logging). The only supported logging framework is SLF4J.

You can use slf4j-jdk14.jar to route SLF4J API calls to JUL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM