简体   繁体   中英

How do I console log sessionID, requestID in node.js?

Need to console log the sessionID, requestID, and timestamp. This code isn't working. Can anyone help me out? Thanks

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

Based on the official Express.js specification , the request object doesn't have an explicit ID or timestamp, but you can introduce it yourselves. The following code will also save the session 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();
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM