Welcome to the Structured Logging example with Spring Boot 3.4! 🎉
This project demonstrates how to enable and configure structured logging in a Spring Boot application, making your logs more readable, searchable, and usable in modern observability tools.
With the release of Spring Boot 3.4, enabling structured logging is easier than ever. This repository provides examples and configurations inspired by the official Spring Blog and the Baeldung article on Java Structured Logging.
- Enable structured logging with simple application properties.
- Choose between different structured logging formats such as ECS (Elastic Common Schema) or Logstash.
- Implement your own formatters for custom log output.
Clone this repository and run the application using:
./gradlew bootRun
Depending on your configuration:
Console Logs: View in your terminal or IDE console.
File Logs: Check the output file log.json in your project directory.
Here are some of the configuration options you can try in your application.properties
file:
//logging.structured.format.console=logstash
//logging.structured.format.file=logstash
//logging.file.name=log.json
//logging.structured.format.console=de.claudioaltamura.spring_boot_structured_logging.MyStructuredLoggingFormatter
//logging.structured.format.console=de.claudioaltamura.spring_boot_structured_logging.MyStructuredLoggingFormatterJson
- This configuration will format console logs in the Logstash format, suitable for ingestion by Logstash or other tools that support the Logstash format.
- This configuration will format file logs in the Logstash format.
- These settings will format logs in the Logstash format and write them to a file named log.json.
- Use a custom formatter class for console logging. Replace MyStructuredLoggingFormatter with your own implementation.
- This configuration uses a custom JSON formatter for console logs, offering full control over the structure and content of your log messages.
logstash
{"@timestamp":"2024-09-06T10:45:47.566949+02:00","@version":"1","message":"Hello structured logging!","logger_name":"de.claudioaltamura.spring_boot_structured_logging.MyLogger","thread_name":"main","level":"INFO","level_value":20000,"userId":"1"}
ecs
{"@timestamp":"2024-09-06T10:46:37.390982Z","log.level":"INFO","process.pid":9954,"process.thread.name":"main","service.name":"spring-boot-structured-logging","log.logger":"de.claudioaltamura.spring_boot_structured_logging.MyLogger","message":"Hello structured logging!","userId":"1","ecs.version":"8.11"}
Spring Blog: Structured Logging in Spring Boot 3.4
Baeldung: Java Structured Logging
Feel free to submit pull requests or raise issues to improve this example project. Contributions are always welcome!