Can I configure the MongoDB Java driver to output useful (for debugging) messages, ideally using one of the standard logging frameworks? I'd mainly be interested in seeing each query that goes out, how much data was received and how long it took, as well as any error codes.

Solution 1

You need to set a couple of system properties before loading any of the MongoDB Java driver classes:

// Enable MongoDB logging in general
System.setProperty("DEBUG.MONGO", "true");

// Enable DB operation tracing
System.setProperty("DB.TRACE", "true");

After doing that the driver will use the standard Java logging framework to log messages.

Unfortunately, as far as I can tell from the Java driver code, the logging granularity is not all that fine - for example you cannot selectively log operations on a specific collection.

Solution 2

Anyone still facing this problem with new version mongodb driver 3.x?

define a logger for mongo driver package in log4j.properties

log4j.logger.org.mongodb.driver=INFO

com.mongodb has changed to org.mongodb.

Solution 3

Another way to do set MongoDB's log level:

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

You don't have to do this before using any of the driver classes, you can set/change log levels at any time.

Solution 4

Following line works for me,

import java.util.logging.Logger;
import java.util.logging.Level;

Logger mongoLogger = Logger.getLogger( "org.mongodb.driver" );
mongoLogger.setLevel(Level.SEVERE); // e.g. or Log.WARNING, etc.

Solution 5

To log all queries with the 3.6 MongoDB Java driver or later:

  1. Make sure you are using slf4j

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.29</version>
    </dependency>
    

    or if you are using log4j2

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.13.0</version>
    </dependency>
    
  2. Set the logging level for org.mongodb.driver to DEBUG

    So for log4j2 you would need to add something like this to the xml configuration file

    <logger name="org.mongodb.driver" level="DEBUG"></logger>
    

Setting the log level to INFO or SEVERE level as suggested in other answers didn't work for me. According to the MongoDB specification if slf4j is not present then

the driver will fall back to JUL (java.util.logging)

which is what most other answers use, so perhaps that uses different log levels (although I can't imagine that's the case)

Solution 6

As of 3.11 beta2 this worked for me

import com.mongodb.diagnostics.logging.Loggers;

import java.util.logging.Level;
import java.util.logging.Logger;


Logger.getLogger(Loggers.PREFIX).setLevel(Level.SEVERE);

Solution 7

Mongodb team offer one solution (https://mongodb.github.io/mongo-java-driver/3.11/driver/reference/monitoring/).

We can implement ConnectionPoolListener and put in when we create MongoClient.

For example (with log4j) :

public class ConnectionPoolListenerMongoDb implements ConnectionPoolListener {
private static final Logger logger = Logger.getLogger(StatisticsDaoImpl.class);

@Override
public void connectionPoolOpened(ConnectionPoolOpenedEvent connectionPoolOpenedEvent) {
    logger.info(connectionPoolOpenedEvent.toString());
}

@Override
public void connectionPoolClosed(ConnectionPoolClosedEvent connectionPoolClosedEvent) {
    logger.info(connectionPoolClosedEvent.toString());
}

@Override
public void connectionCheckedOut(ConnectionCheckedOutEvent connectionCheckedOutEvent) {
    logger.info(connectionCheckedOutEvent.toString());
}

@Override
public void connectionCheckedIn(ConnectionCheckedInEvent connectionCheckedInEvent) {
    logger.info(connectionCheckedInEvent.toString());
}

@Override
public void waitQueueEntered(ConnectionPoolWaitQueueEnteredEvent connectionPoolWaitQueueEnteredEvent) {
    logger.info(connectionPoolWaitQueueEnteredEvent.toString());
}

@Override
public void waitQueueExited(ConnectionPoolWaitQueueExitedEvent connectionPoolWaitQueueExitedEvent) {
    logger.info(connectionPoolWaitQueueExitedEvent.toString());
}

@Override
public void connectionAdded(ConnectionAddedEvent connectionAddedEvent) {
    logger.info(connectionAddedEvent.toString());
}

@Override
public void connectionRemoved(ConnectionRemovedEvent connectionRemovedEvent) {
    logger.info(connectionRemovedEvent.toString());
}
}

Settings :

    private MongoClientSettings getMongoClientSettings() throws IOException {
    return MongoClientSettings.builder()
                    .applyToConnectionPoolSettings(new Block<ConnectionPoolSettings.Builder>() {
                        @Override
                        public void apply(ConnectionPoolSettings.Builder builder) {
                            builder.addConnectionPoolListener(new ConnectionPoolListenerMongoDb());
                        }
                    })
                    .applyConnectionString(new ConnectionString(Settings.getMongoSettings()))
                    .build();
}

Creation :

MongoClientSettings settings = getMongoClientSettings();
        mongoClient = MongoClients.create(settings);