Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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.
–
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
–
–
I am new to Spring Framework, but I added the two entries to application config file
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG
logging.level.org.springframework.data.mongodb.core.ReactiveMongoTemplate=DEBUG
There was no query logged to console.
After that, I enabled DEBUG level for the application and find out log entries for my queries in log output.
2022-09-15 08:55:26.945 DEBUG 4388 --- [ main]
org.mongodb.driver.protocol.command : Sending command '{ "find" : "movies",
"filter" : { "countries" : { "$all" : ["Russia", "Japan"] } },
So I reversed the application log level back to INFO, and enabled DEBUG level for org.mongodb.driver.protocol.command namespace.
logging.level.org.mongodb.driver.protocol.command=DEBUG
Hope this helps someone.
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.