Created
December 7, 2014 21:32
-
-
Save myndzi/7417d940a81739e8b2c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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