how to prevent logging on console when connected to mongodb from java?

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

You could just use

logging.level.org.mongodb.driver: ERROR

i tried this java.util.logging.Logger.getLogger("org.mongodb.driver").setLevel(Level.OFF);

it not worked, it still logs com.mongodb.diagnostics.logging.JULLogger log

I changed it to JULLogger and it worked

java.util.logging.Logger.getLogger("JULLogger").setLevel(Level.OFF);


Thanks to @jyemin By using MongoDB official documentation link

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

Now no logs are there in the console.