Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log MongoDB queries with Spring Boot

Is it possible to log all MongoDB queries in my Spring Boot app? I tried this:

logging.level.org.springframework.data.document.mongodb=INFO
log4j.category.org.springframework.data.document.mongodb=INFO

But it didn't work.

like image 526
Fabio Ebner Avatar asked Aug 30 '16 01:08

Fabio Ebner


2 Answers

The actual queries are logged by the MongoTemplate instance at the DEBUG level.

Setting the log level for org.springframework.data.mongodb.core.MongoTemplate to DEBUG will therefore enable the query logging.

For example, just add this line to your application.propertiese file:

logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

Of course, you can also change the log level using any of the externalized configuration options that Spring Boot provides.

The log will be :

2018-11-26 19:23:16.574 DEBUG 15668 --- [nio-8081-exec-2]
o.s.data.mongodb.core.MongoTemplate : find using query: { "status" : "ACTIVE", "item._id" : { "$oid" : "5bfbcde45ac3366ad70cdb9f" } } fields: Document{{}}

like image 124
Mojtaba Yeganeh Avatar answered Oct 20 '22 06:10

Mojtaba Yeganeh


If using spring boot reactive mongodb, set logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG

like image 23
RJ.Hwang Avatar answered Oct 20 '22 06:10

RJ.Hwang