David Wood
David Wood

Reputation: 434

How do i control mongo java driver logging using java.util.logging properties

I am using the java mongodb driver 3.2.2 (compile group: 'org.mongodb', name: 'mongo-java-driver', version:'3.2.2) and can't seem to turn OFF the logging that is coming from the driver. My program is as follows:

public static void main(String args[]) {
    Enumeration<String> names = LogManager.getLogManager().getLoggerNames();

    Logger l = Logger.getLogger( "org.mongodb.driver" );
    l.info("Hello INFO!");
    l.warning("Hello WARNING!");

    SoundDB db = new SoundDB();
    db.doMain(args);

    while (names.hasMoreElements())
        System.out.println("Name = " + names.nextElement());

    l.info("Hello INFO!");
    l.warning("Hello WARNING!");
}

and when started with -Djava.util.logging.config.file=logging.properties, produces

Oct 12, 2016 7:44:22 PM com.ibm.watson.iot.sound.tools.SoundDB main
WARNING: Hello WARNING!
Loading caa properties from file:/C:/Users/IBM_ADMIN/git/iot-sound/IoT-Sound/caa.properties
19:44:23.889 [main] INFO  org.mongodb.driver.cluster - Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
19:44:23.971 [main] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING}]
19:44:24.030 [main] INFO  org.mongodb.driver.cluster - 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
19:44:24.042 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:1, serverValue:1261}] to localhost:27017
19:44:24.042 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Checking status of localhost:27017
19:44:24.044 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] INFO  org.mongodb.driver.cluster - Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[3, 2, 4]}, minWireVersion=0, maxWireVersion=4, maxDocumentSize=16777216, roundTripTimeNanos=1672627}
19:44:24.046 [cluster-ClusterId{value='57fecad73df6efadcc807d9e', description='null'}-localhost:27017] DEBUG org.mongodb.driver.cluster - Updating cluster description to  {type=STANDALONE, servers=[{address=localhost:27017, type=STANDALONE, roundTripTime=1.7 ms, state=CONNECTED}]
...
Name = javax.management.monitor
Name = javax.management.mlet
Name = org.bson.ObjectId
Name = global
Name = org.mongodb.driver
Name = javax.management
Name = javax.management.mbeanserver
Name = 
Oct 12, 2016 7:44:24 PM com.ibm.watson.iot.sound.tools.SoundDB main
WARNING: Hello WARNING!

logging.properties contains

.level=WARNING
handlers=java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level=ALL

The org.mongodb.driver logger has its level set correctly to WARNING since only my warning messages are being printed out and not the info messages. There is no change (as I would expect) if I add the following to the properties:

org.bson.ObjectId.level=WARNING
org.mongodb.driver.level=WARNING

So, does anyone have any idea what I'm doing wrong? Thanks.

Upvotes: 1

Views: 3608

Answers (1)

Mike Shauneu
Mike Shauneu

Reputation: 3289

From: http://mongodb.github.io/mongo-java-driver/3.2/driver/reference/management/logging/

"By default, logging is enabled via the popular SLF4J API. The use of SLF4J is optional; the driver will use SLF4J if the driver detects the presence of SLF4J in the classpath. Otherwise, the driver will fall back to JUL (java.util.logging)"

Make sure that you have no slf4j dependency in your classpath (directly or through other libs). In case you have slf4j you need to configure slf4j instead of java logging to set-up log level.

slf4j is just logging API, actual logging could be backed by any implementation (JUG, Log4J, logback). See https://dzone.com/articles/how-configure-slf4j-different for additional info. What is actually using is depends on your classpath. If you use maven you could find it by getting dependency hierarchy.

Upvotes: 3

Related Questions