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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答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.
回答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.
回答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.
回答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.