top of page
Search

Advanced logging techniques in Spring Boot

  • Writer: Mark Kendall
    Mark Kendall
  • Dec 2, 2024
  • 2 min read

Advanced logging techniques in Spring Boot can significantly enhance your application's observability, debugging, and monitoring capabilities. Here are some key techniques and best practices:

1. Structured Logging

Structured logging involves logging messages in a structured format like JSON, which makes it easier to parse and analyze logs using tools like ELK Stack (Elasticsearch, Logstash, Kibana) or Splunk.

Logback Configuration for JSON Logging

<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
            <providers>
                <timestamp>
                    <timeZone>UTC</timeZone>
                </timestamp>
                <pattern>
                    <pattern>
                        {
                            "timestamp": "%date{ISO8601}",
                            "level": "%level",
                            "thread": "%thread",
                            "logger": "%logger",
                            "message": "%message",
                            "context": "%mdc"
                        }
                    </pattern>
                </pattern>
            </providers>
        </encoder>
    </appender>
    <root level="INFO">
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

2. Log Levels and Configuration

Configure different log levels for various packages to control the verbosity of logs. This helps in focusing on critical information while avoiding log clutter.

logging.level.root=INFO
logging.level.com.example=DEBUG
logging.file.name=logs/application.log

3. Log Rotation and Archiving

Implement log rotation to manage log file sizes and prevent disk space issues. Logback supports time-based and size-based log rotation.

Logback Configuration for Log Rotation

<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
    <file>logs/application.log</file>
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
        <fileNamePattern>logs/application-%d{yyyy-MM-dd}.log</fileNamePattern>
        <maxHistory>30</maxHistory>
    </rollingPolicy>
    <encoder>
        <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
</appender>
<root level="INFO">
    <appender-ref ref="FILE" />
</root>

4. Custom Loggers and Appenders

Create custom loggers and appenders to direct logs to different destinations, such as external systems like Elasticsearch or Splunk.

Custom Logger Example

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

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

    public void logCustomMessage(String message) {
        logger.info("Custom log message: {}", message);
    }
}

5. Log Correlation

Use Mapped Diagnostic Context (MDC) to add contextual information to logs, such as request IDs or user IDs, which helps in tracing logs across different services.

Using MDC in Spring Boot

import org.slf4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

public class MDCFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        try {
            MDC.put("requestId", UUID.randomUUID().toString());
            filterChain.doFilter(request, response);
        } finally {
            MDC.clear();
        }
    }
}

6. Exception Logging

Ensure that exceptions are logged with sufficient detail to aid in debugging. Use @ControllerAdvice to handle and log exceptions globally.

Global Exception Handler

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

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

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception ex) {
        logger.error("Exception occurred: ", ex);
        return new ResponseEntity<>("An error occurred", HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

7. Monitoring and Alerts

Integrate logging with monitoring tools to set up alerts for specific log patterns or errors. Tools like Prometheus and Grafana can be used for this purpose.

8. Performance Considerations

Be mindful of the performance impact of logging. Avoid excessive logging, especially in high-throughput applications, and use asynchronous logging if necessary.

By implementing these advanced logging techniques, you can enhance the observability and maintainability of your Spring Boot applications[1][2][3][4][5]. If you have any specific questions or need further details, feel free to ask!


References

 
 
 

Recent Posts

See All

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
Post: Blog2_Post

Subscribe Form

Thanks for submitting!

©2020 by LearnTeachMaster DevOps. Proudly created with Wix.com

bottom of page