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.or
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{{}}
This method is a bit longer but it solves much much more. We are going to use a tool called Spring Boot Admin Server.
First you need to include some dependencies
<!--Dependency for registering your app as a Spring Boot Admin Server-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.3.3</version>
</dependency>
<!--Provide a nice looking ui-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.3.3</version>
</dependency>
<!--Dependency for registering your app as a Spring Boot Admin Client-->
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
Enable your app to be a Spring Boot Admin Server using the annotation @EnableAdminServer
.
@SpringBootApplication
@EnableAdminServer
public class Application {
public static void main(String[] args) {
// ... your code as before ...
}
}
In your application.properties
add the following:
Register your app to the Spring Boot Admin Server which is still your app
spring.boot.admin.url=http://localhost:8031
Instruct Spring Boot Admin Server where to find the client
spring.boot.admin.client.service-url=http://localhost:8031
spring.boot.admin.client.management-url=http://localhost:8031
spring.boot.admin.client.health-url=http://localhost:8031/health
In your logback.xml
just add the following line <jmxConfigurator/>
. This allows configuration of logback via JMX. More info here
... and voila you are done. Now you can change the debug level for any logger at runtime.
i. Just visit the url for your Spring Boot Admin Server-in our case here (http:/localhost:8031
).
ii. A list of applications (clients) registered will be displayed on the home page.
iii. Click Details
against the registered clients which will take you to another page.
iv. Click the Logging
tab which will list all loggers registered in your application.
v. You can change the log levels it will change your logging level at runtime. Here is a snippet of what you expect
To answer your question, if you want to see the MongoDB queries, just look for MongoTemplate
and change the logging level to DEBUG
.
For versions 2.*.* of Spring Boot Admin, use the following configurations:
spring.boot.admin.client.url=http://localhost:8031
If using spring boot reactive mongodb, set logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG