如何在node.js中控制日志sessionID,requestID?

时间:2015-10-14 17:38:43

标签: javascript node.js express

需要控制台记录sessionID,requestID和时间戳。这段代码不起作用。谁能帮我吗?感谢

app.use(function(req, res, next) {
  var t_log= console.log;
  console.log = function(){
    arguments[0] = JSON.stringify(arguments[0]) + " sessionID=" + req.sessionID +timestamp+ " requestID=" + req.id;
    t_log.apply(console, arguments);
}  
next();
});

1 个答案:

答案 0 :(得分:0)

根据官方Express.js specification,请求对象没有明确的ID或时间戳,但您可以自己介绍它。以下代码还将保存会话ID:

var uuid = require('node-uuid');

app.use(function (req, res, next) {
    var timestamp = Date.now();
    var sessionID = req.sessionID; // Only available if you've enabled the express-session package.
    var requestID = uuid.v4();

    req.id = requestID; // In case other parts of the code require this ID.

    console.log(timestamp, sessionID, requestID);

    ...

    next();
});