簡體   English   中英

生產者日志時如何添加MDC

[英]How to add MDC when producer logs

我試圖在 MDC 中設置一個值以顯示在每個日志行中,但是當 kafka 生產者中有一個 log.info("") 時,日志不會顯示我之前為 MDC 添加的值。

我有一個攔截器來為correlationId設置一個“默認值”並在請求后清除MDC。

 @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();
}

當它進入控制器時,我設置了 MDC 相關 ID 值。

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

僅適用於 kafka 日志不包括先前添加的 MDC 消息

2022-06-08 10:15:31.616 INFO {correlationId=correlation-id-to-be-set} 19608 --- [nio-8080-exec-2] .acUnsubscribedNotificationsController:已收到請求。 CorrelationId hq12345678888 2022-06-08 10:15:33.734 INFO {} 19608 --- [rest-services-1] cubunapUnsubscribedAlertsProducer :消息成功傳遞 2022-06-08 10:15:33.735 INFO {} 19608 --- [rest -services-1] cubunargos.producers.BpmProducer:進入 BPM sendMessage 事件代碼:bk_argos_sent 2022-06-08 10:15:33.747 INFO {} 19608 --- [rest-services-1] cubunargos.producers.BpmProducer:事件發送到bpm 主題事件代碼:bk_argos_sent 2022-06-08 10:15:33.752 INFO {correlationId=hq12345678888} 19608 --- [nio-8080-exec-2]

因此,正如您在時間戳之后的日志中看到的那樣,{} 內的 INFO {} 應該是僅出現在 nio-8080-exec-2 線程中的 MDC 值,從 BpmProducer 和 UnsubscribedAlertsProducer 觸發的日志(它們是用於kafka 生產事件)不包括在日志中先前添加的 MDC 值。 我的問題是我需要做什么才能在每個日志行中顯示correlationId,如果有任何問題請告訴我,提前謝謝。

你的項目中應該有一個 logback 配置文件(logback-spring.xml)。 在這個文件中,我們可以配置包級別的記錄器。

示例:在以下配置中,將為 com.company 包的 DEBUG 日志打印 mdc 鍵,並為其他包的 INFO 日志打印 mdc 鍵。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM