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.
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: trueCorrect configuration using the logging framework:
logging:
level:
'[org.hibernate.SQL]': DEBUG
'[org.hibernate.orm.jdbc.bind]': TRACESample 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]': DEBUGSample 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 - committing4. JDBC Execution Logging
Configuration for JdbcTemplate/JdbcClient:
logging:
level:
'[org.springframework.jdbc]': TRACETest 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: TRACESpring 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]': TRACESample 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]': DEBUGOutput 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]': TRACESample 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]': DEBUGRunning 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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
