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

时间:2016-10-12 23:51:38

标签: mongodb logging mongodb-java java.util.logging

我正在使用java mongodb驱动程序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记录器的级别设置正确为WARNING,因为只打印了我的警告消息而不是信息消息。如果我将以下内容添加到属性中,则没有任何变化(如我所料):

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

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

1 个答案:

答案 0 :(得分:2)

来自:http://mongodb.github.io/mongo-java-driver/3.2/driver/reference/management/logging/

“默认情况下,通过流行的SLF4J API启用日志记录。使用SLF4J是可选的;如果驱动程序检测到类路径中存在SLF4J,驱动程序将使用SLF4J。否则,驱动程序将回退到JUL( java.util.logging中)“

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

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