title: RestTemplate logging interceptor + Spring Cloud Sleuth
body: <p>I've created a <code>RestTemplate</code> interceptor (<code>ClientHttpRequestInterceptor</code>) to log requests and responses and enabled Spring Cloud Sleuth with the goal of tracing transactions from when the interceptor receives a request to the point where it returns a result. However, Sleuth's trace and span ids do not appear in the interceptors log messages; it seems they are only at a later stage in processing the request. I would like to be able to trace into services that do not have explicit logging, so the interceptor seemed like a good approach, but if there is no way to add the trace and span ids at this stage then I may need to find another approach.  Looking for any suggestion which would either add the trace and span to the interceptors log messages or another way to log requests and responses across services with trace and span ids.  Thanks!</p>
  <p><strong>EDIT</strong>: Here's an example of the log output I'm currently getting from my <code>RestTemplate</code> interceptor with Spring Cloud Sleuth enabled:</p>
  <pre><code>2020-08-21 18:53:17.164 DEBUG [,,,] 14256 --- [           main] .c.o.l.RequestResponseLoggingInterceptor : ⇒ HTTP REQUEST: METHOD 'POST', URI 'http://localhost:59742/….
  </code></pre>
  <p>What I'd really like to get is the request (or repsonse) logged with the Sleuth <code>trace</code> and <code>span</code> ids:</p>
  <pre><code>2020-08-21 18:53:17.164 DEBUG [my-service-name,d3cb8a2fa7d1d9ac,80b023cb829bf254,false] 14256 --- [           main] .c.o.l.RequestResponseLoggingInterceptor : ⇒ HTTP REQUEST: METHOD 'POST', URI 'http://localhost:59742/….
  </code></pre>
  <p>I'm wondering whether this is possible (ie, adding logging to all service through a <code>RestTemplate</code> interceptor <em>and</em> having those log messages contain the Sleuth ids) or whether there is a different approach I should take to log all requests and responses with the Sleuth trace/span ids.  I would appreciate any suggestions!</p>
  <p><strong>EDIT 2</strong>: A bit more information...My Sleuth config at this point is nothing more than enabling it in a <code>bootstrap.yml</code> file:</p>
  <pre><code>spring:
      cloud:
          sleuth:
              enabled: true
  </code></pre>
  <p>I also created / configured a custom <code>CommonsRequestLoggingFilter</code>, which executes just after the interceptor (it's the next line in the log) and <em>does</em> have the trace / span ids in its log message:</p>
  <pre><code>2020-08-21 18:53:17.164 DEBUG [my-app,d3cb8a2fa7d1d9ac,80b023cb829bf254,false] 3479 --- [c-auto-1-exec-2] c.t.c.o.l.CustomRequestLoggingFilter  : ⇒ POST ...
  </code></pre>
  <p>So Sleuth seems to be configured correctly (at least for my purposes now); it just isn't adding the trace ids to the interceptor's log messages.
  Thanks!</p>