Unlocking Real-Time Debugging in Spring Boot 3 with LoggingSystem

Unlocking Real-Time Debugging in Spring Boot 3 with LoggingSystem

I faced a difficulty when debugging issues in production because there wasn’t an easy way to enable or disable debug logs.

We had to modify application properties and restart the service, which is inconvenient in a production specially .(Needs further approval… Oh no!)

Wouldn’t it be great to adjust logs on the go without touching the running app?

We created a secure REST API to handle log configuration, but the issue was that the code was tightly coupled with the specific logging implementation that application was using, like Log4j2 or Logback.

In the edge of microservice and various libraries that use different libraries for logging. This makes it hard to have a single, consistent way to manage logging across all across the microservices/Library.

Imagine typical application uses the SLF4J API for logging, but the actual logging is handled by the Log4j2 or Logback library.

In this case, code would need to interact with specific classes from Log4j2 or Logback to manage the logging levels. (Such a tedious task… hmm)

Spring Boot's LoggingSystem solves this problem by automatically figuring out which logging library is being used and providing a common way to control it. This makes our logging setup more flexible and easier to manage.

This makes our logging REST API more flexible and easier to manage.(can be used as common API)

Here’s a code sample illustrating this..!

  1. Create an simple Spring Application with the given dependency.

package com.example.logging.RealTimeLogAPI;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RealTimeLogApiApplication {

 public static void main(String[] args) {
  SpringApplication.run(RealTimeLogApiApplication.class, args);
 }
}        
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="https://maven.apache.org/POM/4.0.0" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="https://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>3.3.3</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <groupId>com.example.logging</groupId>
 <artifactId>RealTimeLogAPI</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <name>RealTimeLogAPI</name>
 <description>Demo project for Spring Boot</description>
 <properties>
  <java.version>17</java.version>
 </properties>
 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <optional>true</optional>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
 </dependencies>

 <build>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>
</project>        

2. Create an REST api for enable or disable the debug log in real time.

package com.example.logging.RealTimeLogAPI;

import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.logging.LoggingSystem;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/log")
public class RealTimeLogLevelController {

    private final LoggingSystem loggingSystem;

    public RealTimeLogLevelController(LoggingSystem loggingSystem) {
        this.loggingSystem = loggingSystem;
    }

    /**
     *
     * @param loggerName can be ROOT or any valid package name
     * @return Logging level status message
     */
    @GetMapping("/enable-debug")
    public String enableDebug(@RequestParam String loggerName) {
        //In production, it's important to employ the relevant security validation measures.
        loggingSystem.setLogLevel(loggerName, LogLevel.DEBUG);
        return "Debug logging enabled for " + loggerName;
    }

    /**
     *
     * @param loggerName can be ROOT or any valid package name
     * @return Logging level status message
     */
    @GetMapping("/disable-debug")
    public String disableDebug(@RequestParam String loggerName) {
        //In production, it's important to employ the relevant security validation measures.
        loggingSystem.setLogLevel(loggerName, LogLevel.INFO);
        return "Debug logging disabled for " + loggerName;
    }
}        

Enabling runtime debug logs in Spring Boot 3 can greatly enhance our ability to troubleshoot and optimize your applications. By following the steps outlined, we can gain deeper insights into application’s behavior and performance.

If you found this guide helpful, please consider liking this. Your support helps me continue to provide valuable content. Thank you for reading!

要查看或添加评论,请登录

Rajeev Kumar的更多文章

社区洞察

其他会员也浏览了