Configure logging for the MongoDB Java driver
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:
-
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>
-
Set the logging level for
org.mongodb.driver
toDEBUG
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)
hansvb
Updated on February 06, 2020Comments
-
hansvb over 4 years
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.
-
Artem about 11 yearsThis would be better with a package name for logger. Is it JUL? SLF4j?
-
Jan Zyka almost 11 yearsTried that but doesn't work for me. Does this need to be called after mongo has been initialized or something?
-
Jan Zyka almost 11 yearsCorrecting myself, I'm able to set level but not able to set filter.
-
ericsoco almost 11 years@JanZyka I haven't experimented with Filters here, so not sure. You might try calling Logger.getFilter() (docs.oracle.com/javase/6/docs/api/java/util/logging/…) before applying a filter, and modify the returned Filter, rather than creating a new one from scratch. Just a guess.
-
Jan Zyka almost 11 yearsFor 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.
-
Brett almost 11 yearsWondering 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.
-
panza over 8 yearsThis is correct if you use org.mongodb rather than com.mongodb for Mongodriver 3.x or later.
-
user1955934 about 8 yearsIm using mongodb-spring data but this doesn't work, im not getting any logging...
-
user1955934 about 8 yearsI 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..
-
THelper over 4 yearsI suspect this only works for the 2.x MongoDB Java Driver, not for 3.x (at least it doesn't work for me).