Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 549
prasad Avatar asked Apr 05 '15 06:04

prasad


People also ask

Do I need to close MongoDB connection in Java?

This is configurable within the Java code. This means, ideally, you should have one Mongo Client instance per JVM. So, the application's client uses a connection from the pool and returns it back to the connection pool after its usage. There is no need for closing the connection explicitly.

Can I connect MongoDB with Java?

Before you start using MongoDB in your Java programs, you need to make sure that you have MongoDB CLIENT and Java set up on the machine. You can check Java tutorial for Java installation on your machine. Now, let us check how to set up MongoDB CLIENT. You need to download the jar mongodb-driver-3.11.

Is MongoDB thread safe Java?

All Connection methods are thread safe, and Connection handles can be shared between threads.


2 Answers

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.

like image 92
prasad Avatar answered Oct 19 '22 19:10

prasad


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

like image 2
artonbej Avatar answered Oct 19 '22 18:10

artonbej