How to disable mongoDB java driver logging?
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);
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);
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.