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

4

16

I followed this mongodb documentation. Here is my code

public class JMongoDBCDemo
{
    MongoClient mongoClient;
    DB db;
    DBCollection coll;
    public JMongoDBCDemo()
    {
        MongoClient mongoClient = new MongoClient( "localhost" , 27017 );
        db = mongoClient.getDB( "messenger" );
        coll = db.getCollection("users");
        DBObject myDoc = coll.findOne();
        System.out.println(myDoc);
        mongoClient.close();
        System.out.println("Got a collection...");
    }
    public static void main(String[] args){
            JMongoDBCDemo mongoDemo = new JMongoDBCDemo();
    }
}

Below is the output

Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, all=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:1, serverValue:3}] to localhost:27017
Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[2, 6, 1]}, minWireVersion=0, maxWireVersion=2, maxDocumentSize=16777216, roundTripTimeNanos=389140}
Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:4}] to localhost:27017
{ "_id" : { "$oid" : "55201cec68fb70b6affba026"} , "name" : "prasad" , "password" : "123456"} //This is my output
Apr 05, 2015 12:17:47 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Closed connection [connectionId{localValue:2, serverValue:4}] to localhost:27017 because the pool has been closed.
Got a collection... //my output

according to documentation it should print like

{ "_id" : { "$oid" : "55201cec68fb70b6affba026"} , "name" : "prasad" , "password" : "123456"}
Got a collection... 

So can any one please help me to prevent these logs in console.

Spanishamerican answered 5/4, 2015 at 6:54 Comment(3)
This might be helpful to you - #9545841Dimorph
This doc link may be helpful to you: mongodb.github.io/mongo-java-driver/3.0/driver/reference/…Ripping
Any idea why there is "Opened connection .." more than once in log ? For me there are 3 entries whenever i try to connect . Please help .Errantry
S
32

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.

Spanishamerican answered 7/4, 2015 at 0:49 Comment(2)
It would be better to configure this wherever main logging is configured, and SEVERE is almost certainly too restrictive.Absinthism
Which Logger? Like what package?Pippas
D
3

You could just use

logging.level.org.mongodb.driver: ERROR
Dublin answered 15/1, 2020 at 12:25 Comment(0)
M
2

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
Mulvihill answered 21/4, 2020 at 9:27 Comment(0)
T
1

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

Typescript answered 7/8, 2019 at 14:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.