quarkusmdc

quarkus access-logs do not show mdc content after clearing mdc in JAX-RS response filters


I just want to clear MDC content when filter chain is completed. I realized after clearing MDC in LoggingFilter class, Quarkus access logs do not log the content I put in. What I expect is that it should be logged, then ContainerResponseFilter should be triggered. Somehow logging of quarkus access log happens after respose filter of JAX-RS

Do you have any idea how can I change the order? I want to log MDC content in quarkus access log, then clear the MDC.

@Path("/hello")
public class GreetingResource {
    java.util.logging.Logger logger = LogManager.getLogManager().getLogger(GreetingResource.class.getName());

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hello() {
        logger.info("a log message");

        return "Hello RESTEasy";
    }
}
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.ext.Provider;

import org.jboss.logging.MDC;

@Provider
public class LoggingFilter implements ContainerRequestFilter, ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) {
        MDC.put("test", "testValue");

    }

    @Override
    public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) {
        MDC.clear();
    }
} 

logs =>


Solution

  • Quarkus uses Jboss logging internally, so leaving clearing of MDC context to Quarkus solve my issue.

    There is also one discussion related to clearing MDC in Quarkus. https://github.com/quarkusio/quarkus/discussions/27254

    Final LoggingFilter class should be like below to have both java.util.logging(JUL) and quarkus.access.log to have MDC content.

    import javax.ws.rs.container.ContainerRequestFilter;
    import javax.ws.rs.ext.Provider;
    
    import org.jboss.logging.MDC;
    
    @Provider
    public class LoggingFilter implements ContainerRequestFilter{
    
        @Override
        public void filter(ContainerRequestContext requestContext) {
            MDC.put("test", "testValue");
    
        }
    
    }