37

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.

Thilo
  • 257,207
  • 101
  • 511
  • 656

7 Answers7

25

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.

thkala
  • 84,049
  • 23
  • 157
  • 201
  • 2
    Wondering if anyone has tried getting this working in Scala, accessing the java driver via casbah, with an SLF4J logging API? Can't seem to get messages logged to my logging framework when not using JUL. – Brett Jul 18 '13 at 10:52
  • 2
    Im using mongodb-spring data but this doesn't work, im not getting any logging... – user1955934 Mar 23 '16 at 09:10
  • 1
    I suspect this only works for the 2.x MongoDB Java Driver, not for 3.x (at least it doesn't work for me). – THelper Dec 17 '19 at 12:05
21

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.

AsSiDe
  • 1,826
  • 2
  • 15
  • 24
  • 2
    I added this in my log4j.properties and nothing happens when i perform queries.. i dont see any logging except my custom logging msgs that I added.. – user1955934 Mar 23 '16 at 13:14
16

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.

ericsoco
  • 24,913
  • 29
  • 97
  • 127
  • 2
    This would be better with a package name for logger. Is it JUL? SLF4j? – bmargulies Mar 07 '13 at 16:10
  • 4
    Tried that but doesn't work for me. Does this need to be called after mongo has been initialized or something? – Jan Zyka May 23 '13 at 06:03
  • Correcting myself, I'm able to set level but not able to set filter. – Jan Zyka May 23 '13 at 06:10
  • @JanZyka I haven't experimented with Filters here, so not sure. You might try calling Logger.getFilter() (http://docs.oracle.com/javase/6/docs/api/java/util/logging/Logger.html#getFilter()) before applying a filter, and modify the returned Filter, rather than creating a new one from scratch. Just a guess. – ericsoco May 23 '13 at 06:49
  • For the record you need to define the filter on Handler. SO I added handler with filter on it and disabled parent handlers for the logger which solved the problem with event still getting logged by the root logger default appender. – Jan Zyka May 23 '13 at 08:19
  • 2
    This is correct if you use org.mongodb rather than com.mongodb for Mongodriver 3.x or later. – panza Feb 01 '16 at 15:29
8

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.
Hemant Thorat
  • 2,386
  • 20
  • 14
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)

THelper
  • 15,333
  • 6
  • 64
  • 104
0

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);
GalacticRaph
  • 872
  • 8
  • 11
0

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);
Julien
  • 65
  • 4