title: Using express request based variables for logging (correlation id)
body: <p>We are adding correlation id's to our node express app, to tie requests together. This id is sent in the header from other services and our middleware collects the id, creates a bunyan logger with the id and adds it to the request object.</p>
  
  <p>Our routes currently look like the following:</p>
  
  <pre><code> router.get('', (req, res, next) =&gt; {
      service.doSomething(req.params.userid)
        .then(items =&gt; res.json(items))
        .catch((err) =&gt; next(err));
    });
  </code></pre>
  
  <p>The current proposed solution is something like the following:</p>
  
  <pre><code> router.get('', (req, res, next) =&gt; {
      let logger = req.log;
      service.doSomething(req.params.userid, logger)
        .then(items =&gt; res.json(items))
        .catch((err) =&gt; next(err));
    });
  </code></pre>
  
  <p>This would mean that the <code>doSomething</code> and each function it calls would need to accept the logging object as a parameter. This means that nearly each function would need to accept a logging object.</p>
  
  <p>For example:</p>
  
  <pre><code>function add(a, b, log) {
    log.debug("Trivial example");
    return a + b;
  }
  </code></pre>
  
  <p>Is there a better way to do this logging on a per request basis, without passing a logging object to each function?</p>