Skip to content

Instantly share code, notes, and snippets.

@myndzi
Created December 7, 2014 21:32
Show Gist options
  • Save myndzi/7417d940a81739e8b2c0 to your computer and use it in GitHub Desktop.
Save myndzi/7417d940a81739e8b2c0 to your computer and use it in GitHub Desktop.
module.exports = function (app) {
if (!app.config.get('app.logRequests')) { return; }
var mountPaths = app.config.get('swag.mountPaths');
var apiRE = new RegExp('^'+mountPaths.api.replace('/', '\\/')+'\\/');
app.use(function (req, res, next) {
if (!apiRE.test(req.url)) { return next(); }
var start = process.hrtime();
res.on('finish', function () {
var dur = process.hrtime(start);
dur = (dur[0] * 10e9 + dur[1]) / 10e6 + 'ms';
var str = res.statusCode + ' ' + req.url + ' (' + dur + ')';
if (res.statusCode < 400) { app.log.info(str); }
else if (res.statusCode < 500) { app.log.warn(str); }
else { app.log.error(str); }
});
next();
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment