udg*_*tel 6 logging node.js express winston
我正在使用winston记录器.我想在每个请求中具有相同uuid的每个日志中添加uuid.
在app.js
var distributorapp = require('./routes/distributorapp');
app.use('/dstapp',distributorapp);
Run Code Online (Sandbox Code Playgroud)
在路线/ distributorapp.js(中间件)
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
}
};
Run Code Online (Sandbox Code Playgroud)
在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
})
]
});
Run Code Online (Sandbox Code Playgroud)
现在在每个请求中我想生成uuid并将其添加到请求体中.所以我在Middleware中添加了.但是如何在customFileFormatter格式化函数中使用logger.js?
当某人使用此记录器记录任何数据时,我希望在每个日志中通过请求将uuid添加到logger格式化程序中.
如果有人要求logger.js
并且做
logger.info("Hello");
logger.info("Hi");
Run Code Online (Sandbox Code Playgroud)
目前我有以下日志
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
中间件时,来自此请求的每个后续日志都会转到路径dstapp/2017-02-24/test.log
但是当请求来自时我们说它/anotherapp
使用anotherapp
中间件并且此请求的后续日志转到路径anotherapp/2017-02-24/test.log
我已经搜索过每一个地方但找不到任何解决方案在此先感谢
我不得不面对同样的问题.我找到了一个解决方案,使用node-uuid库为每个请求生成一个唯一的uuid,以及continuation-local-storage库,用于在模块之间共享信息.
1°.我添加了一个中间件功能来为每个请求创建uuid并将其添加到我创建的命名空间中:
var uuid = require('node-uuid');
var createNamespace = require('continuation-local-storage').createNamespace;
var myRequest = createNamespace('my request');
// Run the context for each request. Assign a unique identifier to each request
app.use(function(req, res, next) {
myRequest.run(function() {
myRequest.set('reqId', uuid.v1());
next();
});
});
Run Code Online (Sandbox Code Playgroud)
2º.我包装winston库打印获取请求的ID并将其添加到每个日志,如下所示:
var winston = require('winston');
var getNamespace = require('continuation-local-storage').getNamespace;
// Wrap Winston logger to print reqId in each log
var formatMessage = function(message) {
var myRequest = getNamespace('my request');
message = myRequest && myRequest.get('reqId') ? message + " reqId: " + myRequest.get('reqId') : message;
return message;
};
var logger = {
log: function(level, message) {
winstonLogger.log(level, formatMessage(message));
},
error: function(message) {
winstonLogger.error(formatMessage(message));
},
warn: function(message) {
winstonLogger.warn(formatMessage(message));
},
verbose: function(message) {
winstonLogger.verbose(formatMessage(message));
},
info: function(message) {
winstonLogger.info(formatMessage(message));
},
debug: function(message) {
winstonLogger.debug(formatMessage(message));
},
silly: function(message) {
winstonLogger.silly(formatMessage(message));
}
};
module.exports = logger;
Run Code Online (Sandbox Code Playgroud)
有了这2段代码,你就可以得到它.
为了更容易,我创建了一个实现所有这一切的库,你可以将它用作winston,而不用担心所有这些:https: //github.com/davicente/express-logger-unique-req-id
如果你想进一步了解它,你可以看看这篇文章:https://solidgeargroup.com/express-logging-global-unique-request-identificator-nodejs