简体   繁体   English

如何在 MongoDB Java-Driver 中启用日志记录

[英]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.我最近一直在将 MongoDB 4.2 与 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).在我使用版本 (MongoDB) 4.4 创建一个新的 MongoDB 项目后,我还必须使用最新的 java-driver(例如 java-driver 4.1 或 4.2)。 (Btw I don't use maven or grandle). (顺便说一句,我不使用 maven 或grandle)。 Since I got the java-driver sync 4.2 I tried to connect to my database with my java app.由于我获得了 java-driver sync 4.2,我尝试使用我的 java 应用程序连接到我的数据库。 The app worked fine with the combination MongoDB 4.2 & java-driver 3.12.7.该应用程序与 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. com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J 警告:在类路径中找不到 SLF4J。 Logging is disabled for the 'org.mongodb.driver' component 'org.mongodb.driver' 组件的日志记录被禁用

I looked up on MongoDB and found this:我查看了 MongoDB 并发现了这个:

By default, logging is enabled via the popular SLF4J API.默认情况下,通过流行的 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.日志记录是可选的:如果驱动程序检测到类路径中存在 SLF4J API(类 org.slf4j.Logger),驱动程序将使用 SLF4J。 Otherwise, the driver will log a single warning via JUL (java.util.logging) and otherwise logging will be disabled.否则,驱动程序将通过 JUL (java.util.logging) 记录一个警告,否则将禁用日志记录。

Are there any solutions to solve the problem with SLF4J without using SLF4J?有什么解决方案可以在不使用 SLF4J 的情况下解决 SLF4J 的问题? I want to see the logs like before with JUL.我想查看 JUL 之前的日志。 At the moment I'm using IntelliJ as my IDE and OpenJDK15.0.02.目前我使用 IntelliJ 作为我的 IDE 和 OpenJDK15.0.02。

After i updated MongoDB & the java-driver nothing worked for some reason.在我更新 MongoDB 和 java-driver 后,由于某种原因没有任何工作。 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.我在其中放了两个 System.out.println() ,我按预期得到了 Point1 和 Point2 。 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点1

  2. 15, 2021 1:14:43 AM com.mongodb.diagnostics.logging.Loggers shouldUseSLF4J 2021 年 1 月 15 日上午 1:14:43 com.mongodb.diagnostics.logging.Loggers 应该使用 SLF4J

  3. WARNING: SLF4J not found on the classpath.警告:在类路径中找不到 SLF4J。 Logging is disabled for the 'org.mongodb.driver' component 'org.mongodb.driver' 组件的日志记录被禁用

  4. Point2点2

  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. 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}}]集群 state 的客户端视图是 {type=REPLICA_SET,servers=[{address=testcluster-shard-00-00.3lmlx.mongodb.net:27017,type=UNKNOWN,state=CONNECTING,exception={com.Z685A5F7CC75B4796F6C6E00CCDException:PrematurelyF6C6E00SocketReadException:Prem.到达流结束}},{地址=testcluster-shard-00-02.3lmlx.mongodb.net:27017,类型=未知,状态=连接,异常={com.mongodb.MongoSocketReadException:过早到达流结束}}, {address=testcluster-shard-00-01.3lmlx.mongodb.net:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: 过早到达流结束}}]

  6. here are some other reports which are maybe not important..这是其他一些可能不重要的报告。

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

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

  9. at TestClass.main(TestClass.java:27) //it is the line with insertOne在 TestClass.main(TestClass.java:27) //它是与 insertOne 的行

  10. Process finished with exit code 1进程以退出代码 1 结束

The solution to the second problem: MongoDB Atlas-> connection -> add new database user and delete the "old" one.第二个问题的解决方法:MongoDB Atlas->连接->添加新的数据库用户并删除“旧”的用户。 After this it works.在此之后它工作。

This is expected behavior with 4.x Java driver releases.这是 4.x Java 驱动程序版本的预期行为。 The change is documented here :此处记录了更改:

The driver no longer logs via JUL (java.util.logging).驱动程序不再通过 JUL (java.util.logging) 进行记录。 The only supported logging framework is SLF4J.唯一支持的日志框架是 SLF4J。

You can use slf4j-jdk14.jar to route SLF4J API calls to JUL.您可以使用 slf4j-jdk14.jar 将 SLF4J API 调用路由到 JUL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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