I am using winston logger. I want to add uuid to every log that has the same uuid for each request.
In app.js
var distributorapp = require('./routes/distributorapp');
app.use('/dstapp',distributorapp);
In routes /distributorapp.js(Middleware)
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);
req.uuid = uuid();
callHsmRest(req, res);
}
};
In logger.js
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 && 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
})
]
});
Now in every request I want to generate a uuid and add it for the body.So request I added to Middleware. But how will it be available for logger.js in the customFileFormatter formatter?
When someone uses this logger to record any data, I want this uuid to be added to the log formatter in each log through a request.
If someone requires logger.jsand do
logger.info("Hello");
logger.info("Hi");
The following magazines are currently following.
2017-02-24T12: 36: 23 + 05: 30 [INFO] ""
2017-02-24T12: 36: 23 + 05: 30 [INFO] ""
2017-02-24T12: 36: 23 + 05: 30 [INFO] c00d6800-fa5f-11e6-83c2-f531bfc95472 ""
2017-02-24T12: 36: 23 + 05: 30 [INFO] c00d6800-fa5f-11e6-83c2-f531bfc95472 ""
.
, /dstapp, distributorapp Middleware, dstapp/2017-02-24/test.log
let say /anotherapp, anotherapp Middleware, anotherapp/2017-02-24/test.log
,
Advance