Log MongoDB queries with Spring Boot

后端 未结 3 1019
小鲜肉
小鲜肉 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:37

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

    0 讨论(0)
  • 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

      <!--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

    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
    
    0 讨论(0)
  • 2020-12-30 08:45

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

    0 讨论(0)
提交回复
热议问题