0

I have been trying to create a logback.xml logging pattern for my spring-boot project in which I can get some requirements.

I want to print data in logging pattern (such as process-id, request-id, user request data for a particular API request, user response for the same, etc. ) using MDC but I couldn't find any solution about how MDC communicates with logback.xml and inserts the required value.

I am new in spring-boot and Java development; kindly suggest me some solution or ideas.

suv1sh
  • 43
  • 1
  • 10

2 Answers2

1

You can add values in the MDC using put

MDC.put("process-id", "1");

And then you define a log pattern that uses these values with %X

%X{process-id}

For example you can define the pattern in the application.properties:

logging.pattern.console=%-4r [%thread] %-5level %X{process-id} - msg%n

Or in the logback.xml

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <layout>
      <Pattern>%-4r [%thread] %-5level %X{process-id} - msg%n</Pattern>
    </layout>       
</appender>
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • thanks for your answer sir, but i am not aware of how MDC works, does spring boot support MDC inherently or do we have to configure it with project by injecting externally? – suv1sh Jan 07 '20 at 11:50
  • No this is part of the logger like logback or Log4j. You don't have to do anything. Simply use MDC.put Read more about MDC in the documentation: http://logback.qos.ch/manual/mdc.html – Simon Martinelli Jan 07 '20 at 12:09
1

Hi, I have one Solution, Apache-Log4j given one class is ThreadContext. with the help of this class we can add/remove data in MDC context.

please follow the below steps :

  • Create one Servlet Filter or Spring Given Filter.
  • import ThreadContext calss from 'org.apache.logging.log4j' package.
  • And add '[%X]' in log pattern
  • please follow below code.

     import org.apache.logging.log4j.ThreadContext;
    
     @Component
     public class MyInterceptor extends HandlerInterceptorAdapter {
    
    
     @Override
     public boolean preHandle(HttpServletRequest request, HttpServletResponse 
     response, Object handler) {
    
             RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
    
            long pid = Long.valueOf(bean.getName().split("@")[0]);
    
            ThreadContext.put("process-id", pid);
            ThreadContext.put("request-id", request.getParameter("requestid"));  //etc.. add Something..
    
    
    return true;
    }
    
      @Override
     public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
        throws Exception {
    
    ThreadContext.put("response", "add here");  //etc.. response.getSomething
    
    ThreadContext.clearMap(); //this remove all in the MDC 
    }
    
    }
    

    Or,
    Instead of this, you can use ServletFilter also.

Ranjith Bokkala
  • 379
  • 1
  • 10
  • can you please explain me how ThreadContext and MDC is similar ? – suv1sh Jan 07 '20 at 12:32
  • I don't know any briefly. but when I am using it is maintain like an application context.we can push the data into MDC context and we can clear the context, until clear the MDC it doesn't remove from context.I hope you understand. – Ranjith Bokkala Jan 07 '20 at 15:23
  • try with the above code once.you will get to know what is happening in the inside. :) – Ranjith Bokkala Jan 07 '20 at 15:25