我如何使用 java.util.logging 属性控制 mongo java 驱动程序日志记录

How do i control mongo java driver logging using java.util.logging properties

我使用的是javamongodb驱动3.2.2(编译组:'org.mongodb',名称:'mongo-java-driver',版本:'3.2.2)可以' 似乎关闭了来自驱动程序的日志记录。 我的程序如下:

public static void main(String args[]) {
    Enumeration<String> names = LogManager.getLogManager().getLoggerNames();

    Logger l = Logger.getLogger( "org.mongodb.driver" );
    l.info("Hello INFO!");
    l.warning("Hello WARNING!");

    SoundDB db = new SoundDB();
    db.doMain(args);

    while (names.hasMoreElements())
        System.out.println("Name = " + names.nextElement());

    l.info("Hello INFO!");
    l.warning("Hello WARNING!");
}

并且当以 -Djava.util.logging.config.file=logging.properties 开始时,会产生

Oct 12, 2016 7:44:22 PM com.ibm.watson.iot.sound.tools.SoundDB main
WARNING: Hello WARNING!
Loading caa properties from file:/C:/Users/IBM_ADMIN/git/iot-sound/IoT-Sound/caa.properties
19:44:23.889 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
19:44:23.971 [main] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]
19:44:24.030 [main] INFO  org.mongodb.driver.cluster - No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
19:44:24.042 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:1261}] to localhost:27017
19:44:24.042 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Checking status of localhost:27017
19:44:24.044 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 4]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=1672627}
19:44:24.046 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=STANDALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=1.7 ms, state=CONNECTED}]
...
Name = javax.management.monitor
Name = javax.management.mlet
Name = org.bson.ObjectId
Name = global
Name = org.mongodb.driver
Name = javax.management
Name = javax.management.mbeanserver
Name = 
Oct 12, 2016 7:44:24 PM com.ibm.watson.iot.sound.tools.SoundDB main
WARNING: Hello WARNING!

logging.properties 包含

.level=WARNING
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level=ALL

org.mongodb.driver 记录器已将其级别正确设置为警告,因为只打印了我的警告消息而不是信息消息。如果我将以下内容添加到属性中,则不会发生任何变化(如我所料):

org.bson.ObjectId.level=WARNING
org.mongodb.driver.level=WARNING

那么,有人知道我做错了什么吗?谢谢。

发件人:http://mongodb.github.io/mongo-java-driver/3.2/driver/reference/management/logging/

"By default, logging is enabled via the popular SLF4J API. The use of SLF4J is optional; the driver will use SLF4J if the driver detects the presence of SLF4J in the classpath. Otherwise, the driver will fall back to JUL (java.util.logging)"

确保您的类路径中没有 slf4j 依赖项(直接或通过其他库)。如果您有 slf4j,则需要配置 slf4j 而不是 java 日志记录以设置日志级别。

slf4j 只是记录 API,实际的记录可以由任何实现(JUG、Log4J、logback)支持。有关更多信息,请参阅 https://dzone.com/articles/how-configure-slf4j-different。实际使用的是什么取决于你的类路径。如果你使用 Maven,你可以通过获取依赖层次结构来找到它。