Logging Transaction IDs in a Spring Web Application Execution Chain

Logging Transaction IDs in a Spring Web Application Execution Chain
Photo by Karsten Winegeart / Unsplash

Introduction

In complex Spring Web applications, tracking requests through the execution chain can be challenging, especially in distributed environments. One effective way to enhance request traceability is by logging transaction IDs throughout the request lifecycle. In this guide, we'll explore how to implement transaction ID logging using SLF4J, Logback, and Spring Web's execution chain.

  1. Implementing Custom Log Layout with Transaction ID: As discussed in our previous post, we can extend LayoutBase<ILoggingEvent> to create a custom layout that includes the transaction ID in log messages. We'll use a filter to extract the transaction ID from the request header and set it in the Mapped Diagnostic Context (MDC), making it available for logging.
  2. Extracting Transaction ID from Request Header: Create a filter to intercept incoming requests and extract the transaction ID from the request header. Set the transaction ID in the MDC for logging purposes.
  3. Customizing Log Layout with Transaction ID: Update the custom layout implementation to include the transaction ID in log messages. Access the transaction ID from the MDC and include it in the log message format.
  4. Logging Transaction ID in Request Execution Chain: With the transaction ID set in the MDC, all log messages generated during the request execution chain will include the transaction ID. This enables easy tracking and correlation of log messages related to a specific request.

A sample implementation. With this implementation, the CustomLayout class serves as both a filter for extracting the transaction ID from the request header and setting it in the MDC, as well as a layout for formatting log messages with the transaction ID. This consolidation simplifies the codebase and eliminates the need for separate filter and layout classes.

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.LayoutBase;
import org.slf4j.MDC;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

public class CustomLayout extends LayoutBase<ILoggingEvent> implements Filter {

    private static final String TRANSACTION_ID_HEADER = "Transaction-ID";

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        try {
            if (request instanceof HttpServletRequest) {
                HttpServletRequest httpRequest = (HttpServletRequest) request;
                String transactionId = httpRequest.getHeader(TRANSACTION_ID_HEADER);
                if (transactionId != null) {
                    MDC.put("transactionId", transactionId);
                }
            }
            chain.doFilter(request, response);
        } finally {
            MDC.remove("transactionId");
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        // Initialization logic
    }

    @Override
    public void destroy() {
        // Cleanup logic
    }

    @Override
    public String doLayout(ILoggingEvent event) {
        String transactionId = MDC.get("transactionId");
        return String.format("[%s] [%s] [%s] - %s%n",
                event.getLevel(),
                event.getLoggerName(),
                transactionId != null ? transactionId : "-",
                event.getMessage());
    }
}

Conclusion

Logging transaction IDs in the request execution chain of a Spring Web application enhances traceability and simplifies debugging in distributed environments. By implementing a custom filter to extract transaction IDs from request headers and integrating them into the logging framework's MDC, developers can easily correlate log messages with specific requests. With this approach, monitoring and troubleshooting complex Spring Web applications become more manageable, leading to improved reliability and performance.

Subscribe to Post, Code and Quiet Time.

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe