2015-04-05 70 views
9


我想學習mongo-java driver.i,緊隨其後mongodb documentation。以下是我的代碼如何防止從java連接到mongodb時在控制檯上登錄?

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

下面是輸出

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 

根據documentation它應打印像

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

所以任何一個可以請幫我,以防止這些日誌中安慰。

+0

這可能對您有所幫助 - http://stackoverflow.com/questions/9545341/configure-logging-for-the-mongodb-java-driver – Vishwas 2015-04-06 11:22:07

+0

此文檔鏈接可能對您有所幫助:http:// mongodb .github.io/mongo-java-driver/3.0/driver/reference/management/logging/ – jyemin 2015-04-06 16:05:42

+0

任何想法爲什麼在日誌中多次出現「Opened connection ..」?對我而言,每當我嘗試連接時,都有3個條目。請幫忙 。 – 2015-11-03 09:41:12

回答

20

由於@jyemin 使用MongoDB official documentation鏈接

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

現在,沒有日誌出現在控制檯中。

+2

最好在配置主要日誌記錄的地方進行配置,而SEVERE幾乎肯定是太嚴格了。 – chrylis 2015-04-15 23:53:23

+0

它幫助了我。謝謝Prasad.Developer – 2015-05-04 07:30:05

相關問題