Skip to content

Instantly share code, notes, and snippets.

@MarZab
Created July 4, 2014 14:43
Show Gist options
  • Save MarZab/a65e0d03fa3c984aa662 to your computer and use it in GitHub Desktop.
Save MarZab/a65e0d03fa3c984aa662 to your computer and use it in GitHub Desktop.
Better logs on Node.js Express.js +Passport.js +Heroku
// create a new token to track logged in users
express.logger.token('user', function(req, res){
// req.user is the deault passport.js variable, log the user id
return req.user ? req.user._id : '-';
});
// use this function on Heroku to get the remote IP
express.logger.token('ip', function(req, res){
return req.headers["x-forwarded-for"] ? req.headers["x-forwarded-for"].split(',').pop() : req.connection.remoteAddress;
});
app.use(express.logger(':ip :user ":method :url" :status :res[Content-Length] :response-time ":referrer"'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment