I am using express.js. I have a need to be able to log certain request data whenever someone tries to log a message. For this I would like to create a helper method like so
function log_message(level, message){
winston.log(level, req.path + "" + message);
}
I would then use the method like so.
exports.index = function(req, res){
log_message("info", "I'm here");
}
Note that I am not passing the req object to the log_message function. I want that to be transparently done so that the log_message API user does not need to be aware of the common data that is being logged.
Is there a way to achieve this with express.js/node.js. Is the request object available from a global variable of some sort?
The following solution is acceptable to me.
In here I have a middleware which adds the log_message method onto the request object. After that I simply call req.log_message to log the message. While this is very similar to passing the req object to every logging call it’s just slightly cleaner.