我如何隐藏java mongodb驱动程序日志记录?

时间:2017-08-10 20:27:06

标签: java mongodb

我使用java mongodb核心。一切都还好,但是。记录信息-.-

[23:17:33] Connecting to MongoDB...
[main] INFO org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
[main] INFO org.mongodb.driver.cluster - No server chosen by ReadPreferenceServerSelector{readPreference=primary} from cluster description ClusterDescription{type=UNKNOWN, connectionMode=SINGLE, serverDescriptions=[ServerDescription{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]}. Waiting for 30000 ms before timing out
[cluster-ClusterId{value='598cbf5e4abca723f8603d80', description='null'}-localhost:27017] INFO org.mongodb.driver.cluster - Exception in monitor thread while connecting to server localhost:27017

Java代码:

 public class MongoDB {
        public MongoClient client = null;
        public Map<String, MongoDatabase> databaseTracker = new HashMap<String, MongoDatabase>();

        public MongoDB(String host, int port) {
            try {
                this.client = new MongoClient( "localhost" , 27017 );
                MongoDatabase database = this.client.getDatabase("Main");

                System.out.println(Arrays.toString(this.getDatabaseNames().toArray()));
            } catch(Exception e){
                System.out.println("MongoDB Connection Error");
            }
        }

        public List<String> getDatabaseNames(){
            List<String> dbs = new ArrayList<String>();
            MongoCursor<String> dbsCursor = this.client.listDatabaseNames().iterator();
            while(dbsCursor.hasNext()) {
                dbs.add(dbsCursor.next());
            }
            return dbs;
        }

        public ServerAddress address() {
            if(this.client != null) {
                return this.client.getAddress();
            }
            return null;
        }
    }

Level.SEVERE没有工作:( 请帮忙,我需要这个。

2 个答案:

答案 0 :(得分:0)

您使用的日志系统是什么?如果是回溯,您可以将org.mongodb.driver.*的日志记录级别设置为高于INFO

答案 1 :(得分:0)

看起来您的日志记录级别是“信息”。如何将您的Loggging级别设置为更高级别

import java.util.logging.Logger;
Logger mongoLogger = Logger.getLogger( "com.mongodb" );
mongoLogger.setLevel(Level.SEVERE); // e.g. or Log.WARNING, etc.

归功于this