itaied
itaied

Reputation: 7107

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.

Upvotes: 29

Views: 45040

Answers (14)

Eel
Eel

Reputation: 1449

I use MongoDB Kotlin Driver in Ktor server and the easiest way to disable those logs (in case of the JVM server)

The Kotlin driver is new and it depends on the Java driver

you will need to go to the resources/logback.xml

and add this <logger name="org.mongodb.driver" level="ERROR"/> to the end before </configuration> so it will be something like this:

<configuration>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{YYYY-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
    <root level="trace">
        <appender-ref ref="STDOUT"/>
    </root>
    <logger name="org.eclipse.jetty" level="INFO"/>
    <logger name="io.netty" level="INFO"/>
    <logger name="org.mongodb.driver" level="ERROR"/>
</configuration>

Upvotes: 1

Ahmed Ashour
Ahmed Ashour

Reputation: 5559

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

logging:
  level:
    org:
      mongodb:
        driver: WARN

Upvotes: 1

yoga
yoga

Reputation: 1959

use the below import

import java.util.logging.Logger;

use the below in code.

Logger mongoLogger = Logger.getLogger( "com.mongodb" );
mongoLogger.setLevel(Level.SEVERE);

Upvotes: 2

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

Upvotes: 0

sagus_helgy
sagus_helgy

Reputation: 1437

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.

Upvotes: 0

krishna Prasad
krishna Prasad

Reputation: 3812

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

Upvotes: 3

derfect
derfect

Reputation: 642

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

Upvotes: 0

chneau
chneau

Reputation: 341

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);

Upvotes: 34

Giorgio Vespucci
Giorgio Vespucci

Reputation: 1586

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.

Upvotes: 1

Mohammad Rafigh
Mohammad Rafigh

Reputation: 796

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.

Upvotes: 7

yuceel
yuceel

Reputation: 1961

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);

Upvotes: 8

Hemant Thorat
Hemant Thorat

Reputation: 2456

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.

Upvotes: 6

Christophe
Christophe

Reputation: 2200

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

Upvotes: 2

itaied
itaied

Reputation: 7107

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.

Upvotes: 7

Related Questions