Master Modern Spring Boot Logging: 8 Essential Scenarios Covered

This guide walks through configuring comprehensive logging in Spring Boot 3.5 for eight common scenarios—including JPA SQL, transaction flow, JDBC operations, RestTemplate, RestClient, WebClient, controller requests, and Feign calls—providing concrete code samples, YAML settings, and sample output to help developers troubleshoot and monitor applications effectively.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Modern Spring Boot Logging: 8 Essential Scenarios Covered

Environment : Spring Boot 3.5.0

1. Introduction

In Spring Boot micro‑service development, logging of database actions, HTTP calls, and service communication is essential for stability and debugging. Traditional SQL logs are slow and lack full request tracing, while transaction and parameter logs are often missing. By standardising logging configuration and adding custom interceptors or event listeners, the full request chain can be captured.

2. JPA SQL Execution Logging

Test code:

// Test code
private final PersonRepository personRepository;
public Person query(Long id) {
  return this.personRepository.findById(id).orElse(null);
}

Typical simple configuration (low‑performance System.out):

spring:
  jpa:
    show-sql: true

Correct configuration using the logging framework:

logging:
  level:
    '[org.hibernate.SQL]': DEBUG
    '[org.hibernate.orm.jdbc.bind]': TRACE

Sample output shows the SQL statement and bound parameters:

23:46:41 DEBUG [main] org.hibernate.SQL Line:135 - select p1_0.id,p1_0.age,p1_0.name from x_person p1_0 where p1_0.id=?
23:46:41 TRACE [main] org.hibernate.orm.jdbc.bind Line:24 - binding parameter (1:BIGINT) <- [1]
Person [id=1, name=Pack_xg, age=22]

3. Transaction Execution Logging

Enable transaction logs:

logging:
  level:
    '[org.hibernate.engine.transaction]': DEBUG

Sample output demonstrates transaction lifecycle events:

23:54:43 DEBUG [main] org.hibernate.engine.transaction.internal.TransactionImpl Line:53 - On TransactionImpl creation, JpaCompliance#isJpaTransactionComplianceEnabled == false
23:54:43 DEBUG [main] org.hibernate.engine.transaction.internal.TransactionImpl Line:81 - begin
23:54:43 DEBUG [main] org.hibernate.SQL Line:135 - select p1_0.id,p1_0.age,p1_0.name from x_person p1_0 where p1_0.id=?
23:54:43 DEBUG [main] org.hibernate.engine.transaction.internal.TransactionImpl Line:98 - committing

4. JDBC Execution Logging

Configuration for JdbcTemplate/JdbcClient:

logging:
  level:
    '[org.springframework.jdbc]': TRACE

Test code:

public Person jdbcQuery(Long id) {
  return this.jdbcTemplate.queryForObject(
    "select * from x_person where id = ?", Person.class, id);
}

Sample output shows the prepared statement and parameter binding:

23:11:33 DEBUG [main] o.s.j.c.JdbcTemplate Line:721 - Executing prepared SQL query
23:11:33 DEBUG [main] o.s.j.c.JdbcTemplate Line:650 - Executing prepared SQL statement [select * from x_person where id = ?]
23:11:33 DEBUG [main] o.s.j.d.DataSourceUtils Line:117 - Fetching JDBC Connection from DataSource
23:11:33 TRACE [main] o.s.j.c.StatementCreatorUtils Line:232 - Setting SQL statement parameter (1:BIGINT) <- [1]

5. RestTemplate Request/Response Logging

Custom interceptor bean:

@Bean
RestTemplateCustomizer restTemplateCustomizer() {
  return restTemplate -> {
    Logger logger = LoggerFactory.getLogger("RestTemplate");
    restTemplate.getInterceptors().add((request, body, execution) -> {
      logger.info("Request: URI={}, Method={}, Headers={}, Body={}",
        request.getURI(), request.getMethod(), request.getHeaders(),
        body != null ? body : "");
      ClientHttpResponse response = execution.execute(request, body);
      String responseBody = StreamUtils.copyToString(response.getBody(), StandardCharsets.UTF_8);
      logger.info("Response: {}", responseBody);
      return new ClientHttpResponse() { /* delegate body */ };
    });
  };
}

Test call and output:

23:00:36 INFO [main] RestTemplate Line:26 - Request: URI: http://localhost:8080/users/666, Method: GET, Headers: [Accept="application/json, application/*+json", Content-Length="0"], Body: []
23:00:36 INFO [main] RestTemplate Line:30 - Response: {"id":666,"name":"姓名 - 666","age":83}
{id=666, name=姓名 - 666, age=83}

6. RestClient Request Logging

@Bean
RestClientCustomizer restClientCustomizer() {
  return builder -> {
    Logger logger = LoggerFactory.getLogger("RestClient");
    builder.requestInterceptors(list -> {
      // same logic as RestTemplate interceptor
    });
  };
}

Running the same request prints similar request/response logs (image omitted for brevity).

7. WebClient Request Logging

@Bean
WebClientCustomizer webClientCustomizer() {
  return builder -> {
    Logger logger = LoggerFactory.getLogger("WebClient");
    builder.filter(new ExchangeFilterFunction() {
      @Override
      public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
        logger.info("Request: URI={}, Method={}, Headers={}", request.url(), request.method(), request.headers());
        return next.exchange(request);
      }
    });
  };
}

Running the request produces logs shown in the accompanying screenshot.

8. Controller Request Logging

Enable full web request tracing:

logging:
  level:
    web: TRACE

Spring MVC automatically logs request details; the console shows each request and the list of defined controller mappings (see screenshots).

9. Controller Parameter Logging

Log only method parameters:

logging:
  level:
    '[org.springframework.web.method]': TRACE

Sample output displays the bound parameters for a GET endpoint.

10. Controller‑Only Logging

Log only controller mapping information:

logging:
  level:
    '[_org.springframework.web.servlet.HandlerMapping.Mappings]': DEBUG

Output shows only the mappings without other web logs.

11. Controller Mapping Matching Logging

Log which controller method matches each request:

logging:
  level:
    '[org.springframework.web.servlet.mvc.method]': TRACE

Sample output illustrates the matched handler method for each incoming request.

12. Feign Call Logging

Configure Feign to emit full request/response details:

public class FeignConfig {
  @Bean
  Logger.Level feignLoggerLevel() {
    return Logger.Level.FULL;
  }
}

@SpringBootApplication
@EnableFeignClients(defaultConfiguration = FeignConfig.class)
public class App { }

logging:
  level:
    '[com.pack.log.feign.UserFeign]': DEBUG

Running a Feign client call prints the complete HTTP exchange (see screenshot).

These configurations together provide a complete, searchable log trail for Spring Boot applications, covering database interactions, HTTP client calls, and controller handling.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaloggingSpring Bootfeignresttemplatewebclientspring-mvcjpa
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.