How to disable mongoDB java driver logging?
Solution 1:
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);
Solution 2:
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);
Solution 3:
So this solved this issue:
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
...
static Logger root = (Logger) LoggerFactory
.getLogger(Logger.ROOT_LOGGER_NAME);
static {
root.setLevel(Level.INFO);
}
You may set the Level
to a higher Level
if you wish to hide all the logs.
Solution 4:
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.