Hello, I am trying to change MongoDB Driver’s Logging level to SEVERE but the older working way to do that, no longer seems to work. Here’s what I’ve tried so far.
@Override
public boolean connect() {
// Changing debugging level for MongoDB Driver
Logger rootLogger = Logger.getLogger("org.mongodb.driver");
rootLogger.setLevel(Level.SEVERE);
ServerApi serverApi = ServerApi.builder()
.version(ServerApiVersion.V1)
.build();
String uri = this.getUri();
MongoClientSettings settings = MongoClientSettings.builder()
.applyConnectionString(new ConnectionString(uri))
.serverApi(serverApi)
.build();
this.mongoClient = MongoClients.create(settings);
this.database = mongoClient.getDatabase(uriBuilder.getDatabase());
try {
Bson command = new BsonDocument("ping", new BsonInt64(1));
Document commandResult = database.runCommand(command);
LoggerUtil.log(
Level.INFO,
LoggerUtil.LogSource.DATABASE,
"Pinged your deployment. You successfully connected to MongoDB!"
);
} catch (MongoException me) {
LoggerUtil.log(
Level.SEVERE,
LoggerUtil.LogSource.DATABASE,
"Error while connecting to MongoDB: " + me.getMessage()
);
return false;
}
return true;
}
Result:
“…[org.mongodb.driver.client] MongoClient with metadata {“application”: {“name”: “WidenCoins”}, “driver”: {“name”: “mongo-java-driver|sync”, “version”: “5.0.0”}, “os”: {“type”: “Linux”, “name”: “Linux”, “architecture”: “amd64”, “version”: “6.5.0-21-generic”}, “platform”: “Java/Private Build/17.0.10+7-Ubuntu-122.04.1”} created with settings MongoClientSettings{readPreference=primary, writeConcern=WriteConcern{w=null, wTimeout=null ms, journal=null}, retryWrites=true, retryReads=true, readConcern=ReadConcern{level=null}, credential=MongoCredential{mechanism=null, userName=‘***’, source=‘admin’, password=, mechanismProperties=}, transportSettings=null, commandListeners=, codecRegistry=ProvidersCodecRegistry{codecProviders=[ValueCodecProvider{}, BsonValueCodecProvider{}, DBRefCodecProvider{},…”