Using Structured Logging in Spring Boot 3.4 with Elastic Common Schema
This article explains how Spring Boot 3.4 introduces structured logging with Elastic Common Schema support, shows how to configure console and file output, create custom formatters, set ECS fields, and use MDC, providing code examples and configuration steps for backend developers.
Preface
Log tracing is an essential step in system fault diagnosis. Starting with Spring Boot 3.4.0, structured logging is supported, offering built‑in formats such as Elastic Common Schema (ECS) and Logstash, while also allowing custom log formats.
Structured Logging
ECS console output
By default, Spring Boot prints logs in a simple text format. To enable ECS‑formatted console logs, add the following property to application.properties :
logging.structured.format.console=ecsAfter the change, the console displays logs in ECS JSON format (see image).
Write structured logs to file
Spring Boot 3.4.0 can also write structured logs to a file. Configure the file output with these properties:
logging.structured.format.file=ecs
logging.file.name=./demo-log.logA log file named demo-log.log will be created, containing ECS‑formatted entries (see image).
Custom structured logging
Spring Boot 3.4.x provides the StructuredLogFormatter interface for custom log formatting. Implement the interface, for example:
package cn.com.ut.cloud.demolog.config;
import ch.qos.logback.classic.spi.ILoggingEvent;
import org.springframework.boot.logging.structured.StructuredLogFormatter;
public class MyStructuredLoggingFormatter implements StructuredLogFormatter
{
@Override
public String format(ILoggingEvent event) {
return "time=" + event.getTimeStamp() + ",level=" + event.getLevel() + ",message=" + event.getMessage() + "\n";
}
}Register the custom formatter in application.properties :
logging.structured.format.console=cn.com.ut.cloud.demolog.config.MyStructuredLoggingFormatterThe logs will now be printed according to the custom format (see image).
Structured logging configuration
Additional ECS fields can be set via properties, for example:
logging.structured.ecs.service.name=MyApp
logging.structured.ecs.service.version=1
logging.structured.ecs.service.environment=Production
logging.structured.ecs.service.node-name=PrimaryThese fields appear in the structured log output (see image).
MDC support
Spring Boot 3.4 also supports MDC (Mapped Diagnostic Context). The following example adds a userId to MDC, logs a message, and then removes the entry:
package cn.com.ut.cloud.demolog.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
class MyLogger implements CommandLineRunner {
private static final Logger logger = LoggerFactory.getLogger(MyLogger.class);
@Override
public void run(String... args) {
MDC.put("userId", UUID.randomUUID().toString());
logger.info("hello world==================");
MDC.remove("userId");
}
}The resulting log entry includes the generated userId (see image).
Conclusion
Structured logging helps developers define clearer log output during development; whether to adopt it depends on the specific requirements of each system.
Source: juejin.cn/post/7447872749756989440
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.