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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

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

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

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

Related

  • How to Introduce a New API Quickly Using Micronaut
  • Keep Your Application Secrets Secret
  • Failure Handling Mechanisms in Microservices and Their Importance
  • How to Build a New API Quickly Using Spring Boot and Maven

Trending

  • Chat With Your Knowledge Base: A Hands-On Java and LangChain4j Guide
  • Traditional Testing and RAGAS: A Hybrid Strategy for Evaluating AI Chatbots
  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • AI Agents: A New Era for Integration Professionals
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Monitoring and Observability
  4. Spring Boot GoT: Game of Trace!

Spring Boot GoT: Game of Trace!

Start with the basics of Spring Boot logs, and learn how to configure logging, test log severity, and adjust log levels using the LoggingSystem class.

By 
Fernando Boaglio user avatar
Fernando Boaglio
·
Sep. 09, 24 · Code Snippet
Likes (2)
Comment
Save
Tweet
Share
5.8K Views

Join the DZone community and get the full member experience.

Join For Free

When we, developers, find some bugs in our logs, this sometimes is worse than a dragon fight!

Let's start with the basics. We have this order of severity of logs, from most detailed to no detail at all:

  • TRACE
  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL
  • OFF

The default severity log for your classes is INFO. You don't need to change your configuration file (application.yaml). 

logging:
  level:
    root: INFO


Let's create a sample controller to test some of the severity logs:

@RestController
@RequestMapping("/api")
public class LoggingController {

    private static final Logger logger = LoggerFactory.getLogger(LoggingController.class);

    @GetMapping("/test")
    public String getTest() {
        testLogs();
        return "Ok";
    }

    public void testLogs() {
        System.out.println(" ==== LOGS ====  ");
        logger.error("This is an ERROR level log message!");
        logger.warn("This is a WARN level log message!");
        logger.info("This is an INFO level log message!");
        logger.debug("This is a DEBUG level log message!");
        logger.trace("This is a TRACE level log message!");
    }

}


We can test it with HTTPie or any other REST client:

Shell
 
$ http GET :8080/api/test
HTTP/1.1 200

Ok


Checking in Spring Boot logs, we will see something like this:

PowerShell
 
 ==== LOGS ====  
2024-09-08T20:50:15.872-03:00 ERROR 77555 --- [nio-8080-exec-5] LoggingController    : This is an ERROR level log message!
2024-09-08T20:50:15.872-03:00  WARN 77555 --- [nio-8080-exec-5] LoggingController    : This is a WARN level log message!
2024-09-08T20:50:15.872-03:00  INFO 77555 --- [nio-8080-exec-5] LoggingController    : This is an INFO level log message!


If we need to change it to DEBUG all my com.boaglio classes, we need to add this info to the application.yaml file and restart the application:

logging:
  level:
    com.boaglio: DEBUG


Now repeating the test, we will see a new debug line:

PowerShell
 
 ==== LOGS ====  
2024-09-08T20:56:35.082-03:00 ERROR 81780 --- [nio-8080-exec-1] LoggingController    : This is an ERROR level log message!
2024-09-08T20:56:35.082-03:00  WARN 81780 --- [nio-8080-exec-1] LoggingController    : This is a WARN level log message!
2024-09-08T20:56:35.083-03:00  INFO 81780 --- [nio-8080-exec-1] LoggingController    : This is an INFO level log message!
2024-09-08T20:56:35.083-03:00 DEBUG 81780 --- [nio-8080-exec-1] LoggingController    : This is a DEBUG level log message!


This is good, but sometimes we are running in production and we need to change from INFO to TRACE, just for quick research. 

This is possible with the LoggingSystem class. Let's add to our controller a POST API to change all logs to TRACE:

@Autowired
private LoggingSystem loggingSystem;

@PostMapping("/trace")
public void setLogLevelTrace() {
    loggingSystem.setLogLevel("com.boaglio",LogLevel.TRACE);
    logger.info("TRACE active");
    testLogs();
}


We are using the LoggingSystem.setLogLevel method, changing all logs from the package com.boaglio to TRACE.

Let's try to call out POST API to enable TRACE:

Shell
 
 $ http POST :8080/api/trace
HTTP/1.1 200 


Now we can check that the trace was finally enabled:

PowerShell
 
2024-09-08T21:04:03.791-03:00  INFO 82087 --- [nio-8080-exec-3] LoggingController    : TRACE active
 ==== LOGS ====  
2024-09-08T21:04:03.791-03:00 ERROR 82087 --- [nio-8080-exec-3] LoggingController    : This is an ERROR level log message!
2024-09-08T21:04:03.791-03:00  WARN 82087 --- [nio-8080-exec-3] LoggingController    : This is a WARN level log message!
2024-09-08T21:04:03.791-03:00  INFO 82087 --- [nio-8080-exec-3] LoggingController    : This is an INFO level log message!
2024-09-08T21:04:03.791-03:00 DEBUG 82087 --- [nio-8080-exec-3] LoggingController    : This is a DEBUG level log message!
2024-09-08T21:04:03.791-03:00 TRACE 82087 --- [nio-8080-exec-3] LoggingController    : This is a TRACE level log message!


And a bonus tip here, to enable DEBUG or TRACE just for the Spring Boot framework (which is great sometimes to understand what is going on under the hood), we can simply add this to our application.yaml: 

Shell
 
debug:true


or 


trace: true


Let the game of trace begin!

API Debug (command) POST (HTTP) shell Spring Boot

Opinions expressed by DZone contributors are their own.

Related

  • How to Introduce a New API Quickly Using Micronaut
  • Keep Your Application Secrets Secret
  • Failure Handling Mechanisms in Microservices and Their Importance
  • How to Build a New API Quickly Using Spring Boot and Maven

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!