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
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Introduce a New API Quickly Using Micronaut
  • Keep Your Application Secrets Secret
  • Developing Secure REST API Using Spring Boot SSL Bundle Feature
  • Debug Like a Pro in 2025: 10 New Eclipse Java Debugger Features to Enhance Your Productivity (With Spring Boot Examples)

Trending

  • Mocking Kafka for Local Spring Development
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • Exactly-Once Processing: Myth vs Reality
  • The Invisible OOMKill: Why Your Java Pod Keeps Restarting in Kubernetes
  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
6.5K 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
  • Developing Secure REST API Using Spring Boot SSL Bundle Feature
  • Debug Like a Pro in 2025: 10 New Eclipse Java Debugger Features to Enhance Your Productivity (With Spring Boot Examples)

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook