1

I'm trying to set a value into MDC to be shown in every log line but when there's a log.info("") inside kafka producer then the log isn't showing the values I previously added for MDC.

I have an interceptor to set a "default-value" for correlationId & clear MDC after the request.

 @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
                         Object handler) {
    MDC.put("correlationId", "correlation-id-to-be-set");
    return true;
}

@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                       @Nullable ModelAndView modelAndView){
    MDC.clear();
}

I set the MDC correlationId value when it gets into the controller.

    @RequestMapping(value = "/notifications", method = RequestMethod.POST, produces = {"application/json"})
@ResponseStatus(HttpStatus.CREATED)
@RolesAllowed({"ROLE_CREATE_NOTIFICATION"})
@LogMethodExecutionTime
public @ResponseBody
OutboundNotificationEvent createNotification(@Valid @RequestBody OutboundNotificationEvent notificationRequest,
                                             HttpServletRequest request){

    log.info("Request Received. CorrelationId {} ",notificationRequest.getCorrelationId());
    if(notificationRequest.getCorrelationId()!=null)
        MDC.put("correlationId", notificationRequest.getCorrelationId());

    bpmProducer.sendMessage(ArgosUtils.mapNotificationPayloadToBpmEvent(notificationRequest, MSG_RECEIVED, EVENT_RECEIVED));

Only for kafka logs isn't including the MDC msg previously added

2022-06-08 10:15:31.616 INFO {correlationId=correlation-id-to-be-set} 19608 --- [nio-8080-exec-2] .a.c.UnsubscribedNotificationsController : Request Received. CorrelationId hq12345678888 2022-06-08 10:15:33.734 INFO {} 19608 --- [rest-services-1] c.u.b.u.n.a.p.UnsubscribedAlertsProducer : Message successfully delivered 2022-06-08 10:15:33.735 INFO {} 19608 --- [rest-services-1] c.u.b.u.n.argos.producers.BpmProducer : Entering BPM sendMessage EventCode: bk_argos_sent 2022-06-08 10:15:33.747 INFO {} 19608 --- [rest-services-1] c.u.b.u.n.argos.producers.BpmProducer : Event sent to bpm topic EventCode: bk_argos_sent 2022-06-08 10:15:33.752 INFO {correlationId=hq12345678888} 19608 --- [nio-8080-exec-2]

So as you can see in the logs after the timestamp there's an INFO {} inside {} should be the MDC value which is only appearing for nio-8080-exec-2 thread, the logs triggered from BpmProducer and UnsubscribedAlertsProducer (which are classes for kafka producing events) are not including in the log the MDC value previously added. My question is what do I need to do to show the correlationId in every single log line, if there's any question pls let me know, thanks in advance.

user29662
  • 13
  • 3

1 Answers1

-1

You should have a logback configuration file in your project (logback-spring.xml). In this file, we can configure the package level loggers.

Example: In the below configuration, mdc keys will be printed for DEBUG logs of com.company package and will print mdc keys for INFO logs of other packages.

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <appender class="ch.qos.logback.core.rolling.RollingFileAppender" name="RFA">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
      <includeCallerData>true</includeCallerData>
      <includeMdcKeyName>url</includeMdcKeyName>
      <includeMdcKeyName>service</includeMdcKeyName>
      <includeMdcKeyName>userId</includeMdcKeyName>
    </encoder>
    <file>logs/service.log</file>
  </appender>


  <springProfile name="!production">
    <logger additivity="false" level="DEBUG" name="com.company"> //check this
      <appender-ref ref="RFA"/>
    </logger>
    <root level="INFO">
      <appender-ref ref="RFA"/>
    </root>
  </springProfile>

</configuration>
ankit
  • 94
  • 5