1. Overview

When using Spring Data MongoDB, we may need to log to a higher level than the default one. Typically, we may need to see, for example, some additional information such as statement executions or query parameters.

In this short tutorial, we’ll see how to modify the MongoDB logging level for queries.

2. Configure MongoDB Queries Logging

MongoDB Support offers the MongoOperations interface or its primary MongoTemplate implementation to access data, so all we need is to configure a debug level for the MongoTemplate class.

Like any Spring or Java application, we can use a logger library and define a logging level for MongoTemplate .

Typically, we can write in our configuration file something like:

1
<logger name="org.springframework.data.mongodb.core.MongoTemplate" level="DEBUG" />

However, if we’re running a Spring Boot application , we can configure this in our application.properties file:

1
logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG

Likewise, we can use the YAML syntax:

1
2
3
4
5
6
7
8
logging:
  level:
    org:
      springframework:
        data:
          mongodb:
            core:
              MongoTemplate: DEBUG

3. Test Class for Logging

First, let’s create a Book class:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
@Document(collection = "book")
public class Book {

    @MongoId
    private ObjectId id;
    private String bookName;
    private String authorName;

    // getters and setters
}

We want to create a simple test class and check out logs.

To demonstrate this, we use Embedded MongoDB. To be sure, let’s check our dependencies first :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo</artifactId>
    <version>${embed.mongo.version}</version>
    <scope>test</scope>
</dependency>

Finally, let’s define our test class using Spring Boot Test:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@SpringBootTest
@TestPropertySource(properties = { "logging.level.org.springframework.data.mongodb.core.MongoTemplate=DEBUG" })
public class LoggingUnitTest {

    private static final String CONNECTION_STRING = "mongodb://%s:%d";

    private MongodExecutable mongodExecutable;
    private MongoTemplate mongoTemplate;

    @AfterEach
    void clean() {
        mongodExecutable.stop();
    }

    @BeforeEach
    void setup() throws Exception {
        String ip = "localhost";
        int port = 27017;

        ImmutableMongodConfig mongodbConfig = MongodConfig.builder()
          .version(Version.Main.PRODUCTION)
          .net(new Net(ip, port, Network.localhostIsIPv6()))
          .build();

        MongodStarter starter = MongodStarter.getDefaultInstance();
        mongodExecutable = starter.prepare(mongodbConfig);
        mongodExecutable.start();
        mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, port)), "test");
    }
    // tests
}

4. Log Samples

In this section, we’ll define some simple test cases and show the relative logs to test the most common scenarios, such as finding, inserting, updating, or aggregation of Document s.

4.1. Insert

First, let’s start with inserting a single Document :

1
2
3
4
5
Book book = new Book();
book.setBookName("Book");
book.setAuthorName("Author");

mongoTemplate.insert(book);

The logs show in which collection we are inserting. When finding a Document , the id is also logged:

1
2
3
4
[2022-03-20 17:42:47,093]-[main] DEBUG MongoTemplate - Inserting Document containing fields: [bookName, authorName, _class] in collection: book
...
[2022-03-20 17:42:47,144]-[main] DEBUG MongoTemplate - findOne using query: { "id" : { "$oid" : "623759871ff6275fe96a5ecb"}} fields: Document{{}} for class: class com.baeldung.mongodb.models.Book in collection: book
[2022-03-20 17:42:47,149]-[main] DEBUG MongoTemplate - findOne using query: { "_id" : { "$oid" : "623759871ff6275fe96a5ecb"}} fields: {} in db.collection: test.book

4.2. Update

Likewise, when updating a Document :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
Book book = new Book();
book.setBookName("Book");
book.setAuthorName("Author");

mongoTemplate.insert(book);

String authorNameUpdate = "AuthorNameUpdate";

book.setAuthorName(authorNameUpdate);
mongoTemplate.updateFirst(query(where("bookName").is("Book")), update("authorName", authorNameUpdate), Book.class);

We can see the actual updated Document field in the logs:

1
[2022-03-20 17:48:31,759]-[main] DEBUG MongoTemplate - Calling update using query: { "bookName" : "Book"} and update: { "$set" : { "authorName" : "AuthorNameUpdate"}} in collection: book

4.3. Batch Insert

Let’s add an example for a batch insertion:

1
2
3
4
5
6
7
8
9
Book book = new Book();
book.setBookName("Book");
book.setAuthorName("Author");

Book book1 = new Book();
book1.setBookName("Book1");
book1.setAuthorName("Author1");

mongoTemplate.insert(Arrays.asList(book, book1), Book.class);

We can see the number of inserted Document s in the logs:

1
[2022-03-20 17:52:00,564]-[main] DEBUG MongoTemplate - Inserting list of Documents containing 2 items

4.4. Remove

Also, let’s add an example for removing:

1
2
3
4
5
6
7
Book book = new Book();
book.setBookName("Book");
book.setAuthorName("Author");

mongoTemplate.insert(book);

mongoTemplate.remove(book);

We can see in the logs, in this case, the id of the deleted Document :

1
[2022-03-20 17:56:42,151]-[main] DEBUG MongoTemplate - Remove using query: { "_id" : { "$oid" : "62375cca2a2cba4db774d8c1"}} in collection: book.

4.5. Aggregation

Let’s see an example for Aggregation. In this case, we need to define a result class. For example, we’ll aggregate by the author name:

1
2
3
4
5
6
7
8
public class GroupByAuthor {

    @Id
    private String authorName;
    private int authCount;

    // getters and setters
}

Next, let’s define a test case for grouping:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Book book = new Book();
book.setBookName("Book");
book.setAuthorName("Author");

Book book1 = new Book();
book1.setBookName("Book1");
book1.setAuthorName("Author");

Book book2 = new Book();
book2.setBookName("Book2");
book2.setAuthorName("Author");

mongoTemplate.insert(Arrays.asList(book, book1, book2), Book.class);

GroupOperation groupByAuthor = group("authorName")
  .count()
  .as("authCount");

Aggregation aggregation = newAggregation(groupByAuthor);

AggregationResults<GroupByAuthor> aggregationResults = mongoTemplate.aggregate(aggregation, "book", GroupByAuthor.class);

We can see in the logs by which field we have aggregated and what kind of aggregation pipeline:

1
[2022-03-20 17:58:51,237]-[main] DEBUG MongoTemplate - Executing aggregation: [{ "$group" : { "_id" : "$authorName", "authCount" : { "$sum" : 1}}}] in collection book

5. Conclusion

In this article, we looked at how to enable a debug logging level for Spring Data MongoDB.

We’ve defined some common query scenarios and looked at their relative logs while doing some live tests.

As always, the code for these examples is available over on GitHub.

Reference https://www.baeldung.com/spring-boot-mongodb-logging