title: NestJS log unique request trace
body: <p>I want to implement logging in NestJS HTTP service so that each incoming request gets its <code>traceId</code> and it gets logged with the log line. Best would be something like this:</p>
  
  <pre><code>[LogInterceptor, traceId=b57847d0-ed31-11e9-969e-7dd13d8a9c1e] Before getUser at 1570911778049.
  [UsersController, traceId=b57847d0-ed31-11e9-969e-7dd13d8a9c1e] Getting user...
  [UsersService] Looking for user by id [testLocal]...
  </code></pre>
  
  <p>Here, as you can see, I'm missing <code>traceId</code> in <code>UsersService</code> log line because I don't know how to reasonably deal with full vertical using request based logger <strong>without using Scope.REQUEST injection scope</strong>. I would try to avoid cascading all my providers to <code>REQUEST</code> scope for performance reasons.</p>
  
  <p>And I managed to get that far by intercepting incoming request, assigning a traceId to it, instantiating a logger with traceId and attaching it to request. Then in the controller, I inject a custom parameter decorator (with controller class name so it picks up the first part of logger context).</p>
  
  <p>And due to nature of NodeJS, I believe that's as far as I can get. I'm coming from Java world where we could deal with this stuff using Threads and their local variables or different Thread based solutions. What's the best solution for that in this ecosystem? </p>
  
  <p><em>Use case is production grade logging system which is able to correlate logs from different parts of the code based on unique request identifier.</em></p>
  
  <p>EDIT: Some time ago I've seen Async Hooks in NodeJS. I forgot to go in that direction during this investigation. But on quick look, they're still <strong>experimental</strong> feature. Any comments on solutions based on them?</p>