Log MongoDB queries with Spring Boot

后端 未结 3 1038
小鲜肉
小鲜肉 2020-12-30 08:31

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         


        
3条回答
  •  天涯浪人
    2020-12-30 08:39

    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

      
      
          de.codecentric
          spring-boot-admin-server
          1.3.3
      
      
      
      
          de.codecentric
          spring-boot-admin-server-ui
          1.3.3
      
      
      
      
          de.codecentric
          spring-boot-admin-starter-client
          1.3.0
      
      
              org.jolokia
              jolokia-core
      
      
    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 . 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
    

提交回复
热议问题