title: How to add uuid in every winston log node js per request?
body: <p>I am using winston logger. I want to add <strong>uuid</strong> in every logs having same uuid per request.</p>
  
  <blockquote>
    <p>In app.js</p>
  </blockquote>
  
  <pre><code>var distributorapp = require('./routes/distributorapp');
  app.use('/dstapp',distributorapp);
  </code></pre>
  
  <blockquote>
    <p>In routes/distributorapp.js(Middleware)</p>
  </blockquote>
  
  <pre><code>var qs = require('querystring');
  var request = require('request');
  var logger = require('../locallibs/logger');
  var uuid = require('uuid/v1');
  module.exports = {
      mddlw: function (req, res, next) {
          req.bodyData = qs.parse(req.body); // parsing body and added to request object
          req.uuid = uuid(); // Adding uuid to request to available every where throught request
          callHsmRest(req, res); // function to do some logic and handle middleware functionality
      }
  };
  </code></pre>
  
  <blockquote>
    <p>In logger.js</p>
  </blockquote>
  
  <pre><code>var winston = require('winston');
  var fs = require('fs');
  var moment = require('moment');
  var today = moment().format('YYYY-MM-DD');
  
  if (!fs.existsSync(today)) {
      fs.mkdirSync(today);
  }
  
  
  function customFileFormatter(options) {
      console.log(options);
      return options.timestamp() + ' [' + options.level.toUpperCase() + '] ' + (undefined !== options.message ? options.message : '') +
              (options.meta &amp;&amp; Object.keys(options.meta).length ? JSON.stringify(options.meta) : '');
  }
  
  var logger = new (winston.Logger)({
      transports: [
          new (winston.transports.File)({
              timestamp: function () {
                  return moment().format();
              },
              json: false,
              filename: today + '/test.log',
              formatter: customFileFormatter
          })
      ]
  });
  </code></pre>
  
  <p>Now in every request I want to generate uuid and add it to request body.So I have added in Middleware.But how it will be available to logger.js in customFileFormatter formatter function?</p>
  
  <p>When some one uses this logger to log any data i want that uuid to be prepended in logger formatter in every logs through request.</p>
  
  <p>If some one require <code>logger.js</code> and do</p>
  
  <pre><code>logger.info("Hello");
  logger.info("Hi");
  </code></pre>
  
  <p>Currently following I have following logs</p>
  
  <blockquote>
    <p>2017-02-24T12:36:23+05:30 [INFO] "Hello"<br>
      2017-02-24T12:36:23+05:30 [INFO] "Hi"</p>
  </blockquote>
  
  <p>But I want </p>
  
  <blockquote>
    <p>2017-02-24T12:36:23+05:30 [INFO] c00d6800-fa5f-11e6-83c2-f531bfc95472 "Hello"<br>
    2017-02-24T12:36:23+05:30 [INFO] c00d6800-fa5f-11e6-83c2-f531bfc95472 "Hi"</p>
  </blockquote>
  
  <hr>
  
  <p>Also I want to change logger file path according to route middleware.</p>
  
  <p>Currently when request came from <code>/dstapp</code> it uses <code>distributorapp</code> Middleware and every subsequent logs from this request goes to path <code>dstapp/2017-02-24/test.log</code><br>
  But when request came from lets say <code>/anotherapp</code> it uses <code>anotherapp</code> Middleware and subsequent logs from this request goes to path <code>anotherapp/2017-02-24/test.log</code></p>
  
  <p>I have searched through every where but can't find any solution
  Thanks in Advance</p>