How to disable mongoDB java driver logging?

2019-01-19 11:06发布

I am trying to disable log outputs of mongo-java-driver-3.0.0.

I have tried to set those in the beginning of my application, before loading the mongo drivers, but it didn't help.

    // Enable MongoDB logging in general
    System.setProperty("DEBUG.MONGO", "false");

    // Enable DB operation tracing
    System.setProperty("DB.TRACE", "false");  

I am getting this kind of logs:

11:01:15.406 [pool-1-thread-1] DEBUG org.mongodb.driver.protocol.query - Sending query of namespace susudev.Players on connection [connectionId{localValue:2, serverValue:28}] to server localhost:27017
11:01:15.406 [pool-1-thread-1] DEBUG org.mongodb.driver.protocol.query - Query completed

11:01:25.174 [cluster-ClusterId{value='554dbecb1b554f11e86c3a69', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Checking status of localhost:27017
11:01:25.177 [cluster-ClusterId{value='554dbecb1b554f11e86c3a69', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=STANDALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=0.6 ms, state=CONNECTED}]

So my console is completely packed with mongo logs and I cant read anything.

9条回答
乱世女痞
2楼-- · 2019-01-19 11:13

in case you use xml resource to configure Logback you can easily do it by adding this:

<logger name="org.mongodb.driver.cluster" level="OFF" />

to your configuration.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-19 11:15

To turn off all the loggers:

final LogManager lm = LogManager.getLogManager();
for( final Enumeration<String> i = lm.getLoggerNames(); i.hasMoreElements(); ) {
   lm.getLogger( i.nextElement()).setLevel( Level.OFF );
}
查看更多
Anthone
4楼-- · 2019-01-19 11:22

To make this portion of code working you need to have Logback. (If maven project)

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.9.0</version>
</dependency>
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
</dependency>

Then if you only want to disable Mongo driver logging, you should do something like this:

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

Again to be clear, here is the list of import for this code to work:

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

This solution is for mongo java driver 3.0.0 and ^.

Edit: Here is a one liner with level set to ERROR.

((LoggerContext) LoggerFactory.getILoggerFactory()).getLogger("org.mongodb.driver").setLevel(Level.ERROR);
查看更多
Viruses.
5楼-- · 2019-01-19 11:29

I've solved it using ,

import java.util.logging.Logger;
import java.util.logging.Level;

Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
mongoLogger.setLevel(Level.SEVERE); // e.g. or Log.WARNING, etc.
查看更多
闹够了就滚
6楼-- · 2019-01-19 11:33

If you need dynamic approach you can iterate over loggers and set level of them. Or you can set levels manually. Here are mongo driver loggers:

LogManager.getLogger("org.mongodb.driver.connection").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.management").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.cluster").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.protocol.insert").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.protocol.query").setLevel(org.apache.log4j.Level.OFF);
LogManager.getLogger("org.mongodb.driver.protocol.update").setLevel(org.apache.log4j.Level.OFF);
查看更多
够拽才男人
7楼-- · 2019-01-19 11:34

For thoses who still face the problem with the logging, you have to set the log level of org.mongodb.driver to something higher like WARN or ERROR. The class com.mongodb is not used anymore even if it is still written in the logs.

See the last answer of this post: Configure logging for the MongoDB Java driver

查看更多
登录 后发表回答