24

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.

1
  • 1
    Please change the log level to DEBUG and try. Also, please provide which class are you specifically look at? Commented Aug 30, 2016 at 8:33

4 Answers 4

48

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{{}}

Sign up to request clarification or add additional context in comments.

3 Comments

While this might provide an answer to the question, some explanation is required. Please update the question with some explanation of how and why this solution works.
While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
This does not log autogenerated queries (like those from findByXxx()). You see "find using query: {}" and nothing else. I.e. you can't validate that autogeneration is generating the queries you think it generates.
23

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

1 Comment

pretty sure this will break my code style checker with line too long #Jokes
17

This method is a bit longer but it solves much much more. We are going to use a tool called Spring Boot Admin Server.

  1. 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>
    
  2. 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 ...
       }
    }
    
  3. 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
    
  4. 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

Change logging levels at runtime

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

3 Comments

Hi downvoter, kindly share with the stackoverflow community why you down voted. It could help improve the answer.
I didn't downvote, but I guess it was downvoted because it's a bad answer to the question as stated, even if it's useful information to have in general.
I wonder if it's possible for a log change to be propagated to all the nodes in a multi-node environment without having to visit the admin URL of each node and update it one by one. Thank you for sharing! Admin Server is great to have :-)
9

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.

1 Comment

answer should be more concise. The way you found out about it is not important for future readers in this case

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.