Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable mongoDB java driver logging?

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.

like image 567
itaied Avatar asked May 09 '15 08:05

itaied


People also ask

Does MongoDB use log4j?

There are no versions of the MongoDB Java driver using log4j by default.

Does MongoDB run on Java?

Overview. In this article, we'll have a look at integrating MongoDB, a very popular NoSQL open source database with a standalone Java client. MongoDB is written in C++ and has quite a number of solid features such as map-reduce, auto-sharding, replication, high availability etc.

How does MongoDB connect to MongoClient in Java?

To connect: MongoClient client = MongoClients. create("<<MongoDB URI>>"); To connect to MongoDB on your local instance and default port, you can just omit the URI part of the above, or use a URI like 'mongodb://localhost:27017'.


1 Answers

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); 
like image 114
chneau Avatar answered Sep 29 '22 23:09

chneau