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

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

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

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • 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
  • Introduce a New API Quickly Using Spring Boot and Gradle

Trending

  • Enhancing Security With ZTNA in Hybrid and Multi-Cloud Deployments
  • Fixing Common Oracle Database Problems
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  • Understanding and Mitigating IP Spoofing Attacks
  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.7K 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

  • 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
  • Introduce a New API Quickly Using Spring Boot and Gradle

Partner Resources

×

Comments

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
  • [email protected]

Let's be friends: