Skip to content

Describe on how a logger can be customised in the Route-Chain (Java) / modify MDC #3378

@domdorn

Description

@domdorn

I need to customise the logger that gets returned by extractLog( log -> () ) so I can attach the current correlationId / userId / other fields into the MDC. However, I'm unable to accomplish this with the documentation I found. This is what I got so far:

  public static RouteAdapter extractCorrelationId(Function<String, Route> inner) {
    return optionalHeaderValueByName("X-CorrelationId", (corr) -> extractLog(log -> {
      String correlationId = corr.orElseGet(() -> {
        String c = UUID.randomUUID().toString();
        log.debug("request does not have a upstream correlationId, assigning " + c);
        return c;
      });
      return provide(correlationId, inner);
    }));
  }

and I was planning to decorate my root-route as follows:

  public Route createRoute() {
    return extractCorrelationId(corr -> extractLog(log -> {
      LoggingAdapter newLog = log;
      var newMDC = newLog.mdc().$plus(Tuple2.apply("correlationId", corr)); // this is a MDC and not a LoggingAdapter :( 
      // how to bring the newMDC into the newLog ?  because AFAIK the MDC is a immutable list, thus I cannot simply change its values 
      return withLog(newLog, () -> allRoutes());
    }));
  }

Please provide a way on how a valid "LoggingAdapter" can be constructed from Java.

Metadata

Metadata

Assignees

No one assigned

    Labels

    1 - triagedTickets that are safe to pick up for contributing in terms of likeliness of being acceptedhelp wantedIdentifies issues that the core team will likely not have time to work ont:docsIssues related to the documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions