禁用Eclipse中的MongoDB控制台日志记录

时间:2015-12-16 08:56:37

标签: java eclipse mongodb logging mongodb-java

我正在使用Mongo数据库来存储我的Java程序中的信息。 我正在使用Eclipse和MongoDB 3.0 我遇到的问题是,每当我的程序与Mongo进行任何交互时,它都会用JULLogger中的红色文本填充控制台。

我目前没有任何设置记录器级别的代码,因为我在这里或其他地方找到的所有示例都给出了错误:

    Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
    mongoLogger.setLevel(Level.SEVERE); 

   "The method getLogger(String) is undefined for the type Logger"

这是输出:

    Dec 16, 2015 8:49:58 AM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Cluster created with settings {hosts=[127.0.0.1:27017],     
    mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
    Dec 16, 2015 8:49:58 AM com.mongodb.diagnostics.logging.JULLogger log
    INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description  
    ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=127.0.0.1:27017, 
    type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
    Dec 16, 2015 8:49:58 AM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Opened connection [connectionId{localValue:1, serverValue:117}] to 127.0.0.1:27017
    Dec 16, 2015 8:49:58 AM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Monitor thread successfully connected to server with description 
    ServerDescription{address=127.0.0.1:27017, type=STANDALONE, 
    state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 0, 7]}, 
    minWireVersion=0, maxWireVersion=3, electionId=null,    
    maxDocumentSize=16777216, roundTripTimeNanos=587005}
    Dec 16, 2015 8:49:58 AM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Opened connection [connectionId{localValue:2, serverValue:118}] to 127.0.0.1:27017
    Document{{_id=567045259311932a6406b4e2, time=16:51:48, data=[31C, 38C, 20C]}}
    Dec 16, 2015 8:49:58 AM com.mongodb.diagnostics.logging.JULLogger log
    INFO: Closed connection [connectionId{localValue:2, serverValue:118}] to        
    127.0.0.1:27017 because the pool has been closed.

有人可以告诉我如何从我的控制台中删除所有Mongo日志吗?

3 个答案:

答案 0 :(得分:4)

非常感谢罗斯的建议:

java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(java.util.log‌​ging.Level.SEVERE);

事实上,正确的代码是这样的,所以只有一个小小的改变:

java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(Level.SEVERE);

我可以确认在运行时我不再在控制台中从mongo接收任何日志。

答案 1 :(得分:0)

您可以尝试此解决方案只需在您的pom文件中添加以下依赖项或手动添加此jar

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-nop</artifactId>
    <version>1.7.22</version>
</dependency>

阅读这些

  1. http://grokbase.com/t/gg/mongodb-user/153eg0pczd/deactivate-logging-in-the-java-driver
  2. https://www.slf4j.org/manual.html#swapping
  3. 获取更详尽的解释

    我遇到了类似的问题,对我有用。

答案 2 :(得分:0)

我尝试了上述所有方法,但均无济于事。最后成功的是

最终为我工作的人如下。

LoggerContext loggerContext = (LoggerContext)LoggerFactory.getILoggerFactory();
loggerContext.getLogger("org.mongodb.driver").setLevel(Level.ERROR);

请同时注意进口:

    import ch.qos.logback.classic.Level;
    import ch.qos.logback.classic.LoggerContext;
    import org.slf4j.LoggerFactory;