NodeJS Express-全局唯一请求ID [英] NodeJS Express - Global Unique Request Id

查看:546
本文介绍了NodeJS Express-全局唯一请求ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以定义每个日志语句中包含的唯一请求ID,而无需将记录器交给每个方法/函数调用?

Is it possible to define a unique request Id that is included in each log statement without handing the logger to each method/function call?

使用的技术:NodeJS,Express,Winston

Technologies in use: NodeJS, Express, Winston

推荐答案

已编辑

最后,我创建了一个库,可以完成所有工作. https://github.com/davicente/express-logger-unique-req-id

Finally, I have created a library that makes all the work. https://github.com/davicente/express-logger-unique-req-id

它是Winston库的包装,因此您可以以相同的方式使用它.

It is a wrapper of Winston library, so you can use it the same way.

让我知道它是否对您有帮助

Let me know if it helps you

我们在多个项目中都遇到了同样的问题,但我找不到针对该问题的完整解决方案.我们正在使用相同的技术(Node.js,Express.js和Winston用于日志) 我找到了使用几个库并包装Winston库的解决方案: -用于为每个请求创建唯一标识符的node-uuid -continuation-local-storage,用于在不同模块之间共享此ID,而无需在所有调用中发送req对象.

We had this same problem in several projects, and I couldn't finde any complete solution for this question. We are using same technologies (Node.js, Express.js and Winston for logs) I found a solution to this using a couple of libraries and wrapping Winston library: - node-uuid for creating unique identificators for each request - continuation-local-storage for sharing this IDs among different modules without sending req object in all the calls.

首先,我需要为每个请求创建并设置唯一标识符.我是在中间件中完成的:

First I need to create and set the unique identificator with each request. I do it in the middleware:

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();
    });
});

此后,我不得不包装Winston库,从上下文中恢复ID并添加到日志消息中:

After that I had to wrap Winston library, recovering the id from the context and adding to the message of the log:

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;

我认为这有点复杂,所以我决定将其写下来.您可以从此处获取更多信息: Express.js:使用全局唯一记录信息请求ID – Node.js

I think it was a little bit complex, so I decided to write it down in a post. You can get more information from there: Express.js: Logging info with global unique request ID – Node.js

我希望这可以解决您的问题.

I hope this helps with your problem.

这篇关于NodeJS Express-全局唯一请求ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆