From 5f95d6f72b5cf946673db6da5eb44f72218610d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Mon, 30 Apr 2018 15:25:54 +0200 Subject: [PATCH 1/6] Log response body --- index.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 335a24f..5dac811 100644 --- a/index.js +++ b/index.js @@ -22,11 +22,26 @@ module.exports = function logRequest(options) { res.setHeader(headerName, id); req.log.info(startOpts, 'start request'); - + + let res_body + const res_send = res.send + res.send = function(body) + { + res_body = body + + res_send.call(res, body) + } + var time = process.hrtime(); res.on('finish', function responseSent() { var diff = process.hrtime(time); - req.log.info({res: res, duration: diff[0] * 1e3 + diff[1] * 1e-6}, 'end request'); + const endOpts = {res: res, duration: diff[0] * 1e3 + diff[1] * 1e-6} + + if (res_body !== undefined) { + endOpts.body = res_body; + } + + req.log.info(endOpts, 'end request'); }); next(); From 4d85c8e148cc1c83240a32d833aef64f8cc75666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Mon, 30 Apr 2018 15:31:12 +0200 Subject: [PATCH 2/6] Code clean-up & update of dependencies --- .gitignore | 1 + index.js | 28 +++++++++++++++------------- package.json | 10 +++++----- 3 files changed, 21 insertions(+), 18 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/index.js b/index.js index 5dac811..ee533d6 100644 --- a/index.js +++ b/index.js @@ -1,27 +1,29 @@ -var uuid = require('uuid'); +const uuid = require('uuid'); -module.exports = function logRequest(options) { - var logger = options.logger; - var headerName = options.headerName || 'x-request-id'; +module.exports = function logRequest({headerName='x-request-id', logger}={}) { return function (req, res, next) { - var id = req.headers[headerName] || uuid.v4(); - var now = Date.now(); - var startOpts = {req: req}; + const id = req.headers[headerName] || uuid.v4(); - req.log = logger.child({ + const log = logger.child({ type: 'request', id: id, serializers: logger.constructor.stdSerializers }); + // Request + req.log = log + + const startOpts = {req: req}; + if (req.body) { startOpts.body = req.body; } - res.setHeader(headerName, id); + log.info(startOpts, 'start request'); - req.log.info(startOpts, 'start request'); + // Response + res.setHeader(headerName, id); let res_body const res_send = res.send @@ -32,16 +34,16 @@ module.exports = function logRequest(options) { res_send.call(res, body) } - var time = process.hrtime(); + const time = process.hrtime(); res.on('finish', function responseSent() { - var diff = process.hrtime(time); + const diff = process.hrtime(time); const endOpts = {res: res, duration: diff[0] * 1e3 + diff[1] * 1e-6} if (res_body !== undefined) { endOpts.body = res_body; } - req.log.info(endOpts, 'end request'); + log.info(endOpts, 'end request'); }); next(); diff --git a/package.json b/package.json index c7175b5..9a8aa32 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,13 @@ "description": "Request, response logger middleware using bunyan. Also provides request<>response duration.", "main": "index.js", "dependencies": { - "uuid": "^2.0.1" + "uuid": "^3.2.1" }, "devDependencies": { - "body-parser": "^1.9.0", - "bunyan": "^1.1.3", - "supertest": "^0.14.0", - "tape": "^3.0.0" + "body-parser": "^1.18.2", + "bunyan": "^1.8.12", + "supertest": "^3.0.0", + "tape": "^4.9.0" }, "repository": { "type": "git", From cb7ffb34bccb24dfe242bf19db7bbaa8af76029f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Thu, 21 Jun 2018 17:39:50 +0200 Subject: [PATCH 3/6] Get request `text` field if `body` is not available --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index ee533d6..7873f0c 100644 --- a/index.js +++ b/index.js @@ -19,6 +19,9 @@ module.exports = function logRequest({headerName='x-request-id', logger}={}) { if (req.body) { startOpts.body = req.body; } + else if (req.text) { + startOpts.text = req.text; + } log.info(startOpts, 'start request'); From d2a5b53c877a2cef332537ea9c6e60b8e42d0059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Thu, 21 Jun 2018 17:40:19 +0200 Subject: [PATCH 4/6] Remove checking for Node.js 0.11 in test (all directions now are IPv6) --- test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test.js b/test.js index b2be554..009f0be 100644 --- a/test.js +++ b/test.js @@ -59,7 +59,7 @@ test('request, response', function(t) { 'user-agent': userAgent, 'connection': 'close' }, - remoteAddress: /^v0\.11/.test(process.version) ? '::ffff:127.0.0.1' : '127.0.0.1', + remoteAddress: '::ffff:127.0.0.1', remotePort: requestLog.req.remotePort }, 'request log have the right `req` property'); }); From b2718258e5cf41e9734aa822d952834970de18b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Thu, 21 Jun 2018 17:40:27 +0200 Subject: [PATCH 5/6] Updated dependencies --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 9a8aa32..7b7963c 100644 --- a/package.json +++ b/package.json @@ -7,10 +7,10 @@ "uuid": "^3.2.1" }, "devDependencies": { - "body-parser": "^1.18.2", + "body-parser": "^1.18.3", "bunyan": "^1.8.12", - "supertest": "^3.0.0", - "tape": "^4.9.0" + "supertest": "^3.1.0", + "tape": "^4.9.1" }, "repository": { "type": "git", From e99c381853b7e1f549a8b03eef3f1f273be88165 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Legan=C3=A9s-Combarro=20=27piranna?= Date: Thu, 21 Jun 2018 17:46:18 +0200 Subject: [PATCH 6/6] Use latest Node.js in TravisCI --- .travis.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 05d299e..2197832 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,3 @@ language: node_js node_js: - - "0.10" - - "0.11" + - "node"