Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure logging for the MongoDB Java driver

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.

like image 477
Thilo Avatar asked Mar 03 '12 10:03

Thilo


People also ask

Does MongoDB use log4j?

There are no versions of the MongoDB Java driver using log4j by default.

Can you use JDBC with MongoDB?

Using the MongoDB JDBC connectivity, it's easier to place a query with the database, introduce updates to the database, and call upon stored processes.

What is MongoDB driver sync?

The MongoDB Driver mongodb-driver-sync is the synchronous Java driver containing only the generic MongoCollection interface that complies with a new cross-driver CRUD specification. It does not include the legacy API (e.g. DBCollection ).


1 Answers

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.

like image 147
thkala Avatar answered Oct 06 '22 00:10

thkala