DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • Querydsl vs. JPA Criteria, Part 6: Upgrade Guide To Spring Boot 3.2 for Spring Data JPA and Querydsl Project
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot

Trending

  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  • Event Driven Architecture (EDA) - Optimizer or Complicator
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 2
  • SaaS in an Enterprise - An Implementation Roadmap
  1. DZone
  2. Coding
  3. Frameworks
  4. Structured Logging in Spring Boot 3.4 for Improved Logs

Structured Logging in Spring Boot 3.4 for Improved Logs

Comparison of structured logging with traditional logging, its uses, and advancements in Spring Framework 6.2 and Spring Boot 3.4.

By 
Karthik Kamarapu user avatar
Karthik Kamarapu
·
Updated by 
Kali Rama Krishna Vucha user avatar
Kali Rama Krishna Vucha
·
Jan. 21, 25 · Analysis
Likes (6)
Comment
Save
Tweet
Share
5.5K Views

Join the DZone community and get the full member experience.

Join For Free

Structured logging has become essential in modern applications to simplify the analysis of logs and improve observability. Spring Boot 3.4 extends the logging capabilities of Spring Framework 6.2. This can be easily configured log formats using application.yml or application.properties.

Before jumping into the details of the improvements, below is a brief on how structured logging has evolved, with comparisons between traditional logging and structured logging in Spring Framework 6.2 and Spring Boot 3.4.

Traditional Logging

Traditional logging relies on string concatenation or placeholders in log messages. This approach was simple but lacked the ability to structure data in a way that log aggregation tools could parse easily.

Java
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TraditionalLogging {
    private static final Logger logger = LoggerFactory.getLogger(TraditionalLogging.class);

    public static void main(String[] args) {
        String userId = "12345";
        String action = "login";
        logger.info("User action: userId={}, action={}", userId, action);
    }
}


Logged as below:

2025-01-10 10:00:00 INFO TraditionalLogging: User action: userId=12345, action=login


The above logging is hard to parse for automated systems, and manual effort is required to extract and analyze the fields, e.g., userId, and action.

Structured Logging

With structured logging, the logs are in machine-readable format (e.g., JSON), making it easier to process and analyze. With Spring Framework 6.2 and Spring Boot 3.4, we can use structured logging schemas like Elastic Common Schema (ECS) and Graylog Extended Log Format (GELF). This means structured logging generates logs as key-value pairs or JSON objects with minimal setup.

Add the below configuration to application.yml to ensure the logs are generated in ECS format.

YAML
 
logging:
  ecs:
    enabled: true
  level:
    root: INFO


Below is an example of logging structured information using SLF4J. Spring handles converting the log messages into a JSON format automatically.

Java
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StructuredLoggingExample {
    private static final Logger logger = LoggerFactory.getLogger(StructuredLoggingExample.class);

    public static void main(String[] args) {
        logger.info("User logged in", Map.of("userId", "12345", "action", "login"));
    }
}


Output in ECS format (JSON):

JSON
 
{
  "@timestamp": "2025-01-10T10:00:00Z",
  "log.level": "INFO",
  "message": "User logged in",
  "userId": "12345",
  "action": "login",
  "logger": "StructuredLoggingExample",
  "ecs.version": "1.2.0"
}


Advantages of Structured Logging

  • Tools like Kibana, Grafana, and Splunk can directly ingest structured logs for alerts.
  • Simplified debugging with the ability to search for specific log entries (e.g., filter by userId)
  • Structured logging aligns with modern logging standards like ECS, which makes it easier to integrate with distributed systems.

Machine-Readability

Let's look at an example of how structured logging helps with machine readability.

Step 1: Structured logs are ingested into tools like:

  • Elastic Search: Parses JSON logs and indexes fields for search.
  • Grafana Loki: Stores structured logs for querying.
  • Splunk: Processes and visualizes log data.

Step 2: Field-based indexing

Each field in a log entry (e.g., userId, action) is indexed separately. This allows tools to perform operations like:

  • Filtering: Shows logs where userId = 12345
  • Aggregating: Count login actions(action = login).

Finally, tools like Kibana or Grafana can create dashboards or alerts based on the log fields.

Log entries:

JSON
 
[
  {"@timestamp": "2025-01-10T12:00:00Z", "action": "login", "userId": "123"},
  {"@timestamp": "2025-01-10T12:01:00Z", "action": "logout", "userId": "123"},
  {"@timestamp": "2025-01-10T12:02:00Z", "action": "login", "userId": "456"}
]


Filters in Kibana/Elasticsearch:

JSON
 
{
  "query": {
    "term": {
      "userId": "12345"
    }
  }
}


Result: All logs where userId = 12345.

Aggregating logs:

JSON
 
{
  "aggs": {
    "login_count": {
      "terms": {
        "field": "action.keyword"
      }
    }
  }
}


Output:

JSON
 
{
  "aggregations": {
    "login_count": {
      "buckets": [
        {"key": "login", "doc_count": 2},
        {"key": "logout", "doc_count": 1}
      ]
    }
  }
}


Conclusion

Structured logging enables developers to create machine-readable logs that are easy to index, analyze, and manage with advanced observability tools. With the latest updates in Spring Framework 6.2 and Spring Boot 3.4, and with built-in support for widely used formats like ECS and GELF, adopting structured logging has become easier and more effective. Beyond improving readability, these improvements enhanced debugging, monitoring, and system analytics, making it an essential practice for building scalable distributed environments. 

Machine Observability Spring Framework Framework Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • Actuator Enhancements: Spring Framework 6.2 and Spring Boot 3.4
  • Querydsl vs. JPA Criteria, Part 6: Upgrade Guide To Spring Boot 3.2 for Spring Data JPA and Querydsl Project
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • How To Validate HTTP Post Request Body - Restful Web Services With Spring Framework | Spring Boot

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!