27

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.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
itaied
  • 6,827
  • 13
  • 51
  • 86

13 Answers13

33

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);
chneau
  • 331
  • 3
  • 5
  • Men you saved my day. I tried everything and this is the only solution which worked – Geoffrey Oct 29 '17 at 13:38
  • @arunkumarsambu Deleting `implementation group: 'org.slf4j', name: 'slf4j-simple', version: '1.8.0-beta2'` from `build.gradle` helps for me. – Polv Jan 10 '19 at 09:12
  • I think the magic here is `.getLogger("org.mongodb.driver")`. This does not seem to be documented anywhere. Thanks for the pointer. – barrypicker Jan 31 '23 at 19:14
8

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);
Aubin
  • 14,617
  • 9
  • 61
  • 84
yuceel
  • 1,943
  • 2
  • 18
  • 27
  • 1
    am using log4j and I did it by setting log4j property: props.setProperty("log4j.logger.org.mongodb.driver", "WARN"); – Avinash Sahu Feb 24 '16 at 05:20
7

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.

itaied
  • 6,827
  • 13
  • 51
  • 86
6

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.
Hemant Thorat
  • 2,386
  • 20
  • 14
6

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.

Mohammad Rafigh
  • 757
  • 9
  • 17
3

To disabled the debug logging into spring boot application of Mongo Driver, set the below configuration in the logback-spring.xml as below:

<logger name="org.mongodb" level="WARN" />

This will disable the logging into console logger as well as other loggers defined in the configuration file logback-spring.xml

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
2

use the below import

import java.util.logging.Logger;

use the below in code.

Logger mongoLogger = Logger.getLogger( "com.mongodb" );
mongoLogger.setLevel(Level.SEVERE);
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
yoga
  • 1,929
  • 2
  • 15
  • 18
2

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

Community
  • 1
  • 1
Christophe
  • 2,131
  • 2
  • 21
  • 35
1

In my case, MongoDB driver 3.5.0, SLF4j+Log4j12, it's been sufficient adding:

log4j.logger.org.mongodb.driver=OFF

in my Log4j .properties configuration file.

Giorgio Vespucci
  • 1,586
  • 3
  • 18
  • 30
0

import your Mongo client through "com.mongodb.client.MongoClient"

import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;

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

public class Quick
{

    public static void main(String[] args)
    {
        Logger.getLogger("org.mongodb.driver").setLevel(Level.WARNING);
        try (MongoClient mongo = MongoClients.create())
        {
            mongo.listDatabaseNames().forEach((Consumer<String>) System.out::println);
        }
    }
}

make sure you have the latest version of the driver, 3.12.2 at the time I wrote this answer

<dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
        <version>3.12.2</version>
</dependency>

if the above doesn't work, you're probably using a different logging module, look up how to turn that off, for example if you're using slf4j, create a file named "simpleLogger.properties" inside your resources folder and add this line to it

org.slf4j.simpleLogger.defaultLogLevel = warn
derfect
  • 622
  • 2
  • 6
  • 14
0

You can use logback.xml for that purpose. For example, my file

<configuration>
    <logger name="org.mongodb.driver.cluster" level="INFO"/>
    <logger name="org.mongodb.driver.protocol" level="INFO"/>
    <logger name="org.mongodb.driver.connection" level="INFO"/>
    <logger name="org.mongodb.driver.operation" level="INFO"/>
</configuration>

Put it into the resources folder.

sagus_helgy
  • 1,417
  • 1
  • 18
  • 30
0

Using slf4j with simplelogger.properties file, I just added this line and it worked:

org.slf4j.simpleLogger.log.org.mongodb.driver=error

General log level stays in another line:

org.slf4j.simpleLogger.defaultLogLevel=info
0

With Spring Boot, in application.yaml (you can also set it to OFF):

logging:
  level:
    org:
      mongodb:
        driver: WARN
Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56