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

129 Views Asked by At

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 =>

  • {"timestamp":"2023-11-24T16:06:08.422+03:00","sequence":1994,"loggerClassName":"org.jboss.logmanager.Logger","loggerName":"org.acme.GreetingResource","level":"INFO","message":"a log message","threadName":"executor-thread-0","threadId":85,"mdc":{"test":"testValue"},"ndc":"","hostName":"hj9xgjxrql","processName":"code-with-quarkus-dev.jar","processId":12056}
  • {"timestamp":"2023-11-24T16:06:08.434+03:00","sequence":1995,"loggerClassName":"org.jboss.logging.Logger","loggerName":"io.quarkus.http.access-log","level":"INFO","message":"127.0.0.1 - - 24/Nov/2023:16:06:08 +0300 "GET /hello-resteasy HTTP/1.1" 200 14","threadName":"executor-thread-0","threadId":85,"mdc":{},"ndc":"","hostName":"hj9xgjxrql","processName":"code-with-quarkus-dev.jar","processId":12056}
1

There are 1 best solutions below

0
C. Bilgin On BEST ANSWER

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");

    }

}