Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to log response body & code clean-up #5

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
language: node_js
node_js:
- "0.10"
- "0.11"
- "node"
46 changes: 33 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
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;
}
else if (req.text) {
startOpts.text = req.text;
}

log.info(startOpts, 'start request');

// Response
res.setHeader(headerName, id);

req.log.info(startOpts, 'start request');

var time = process.hrtime();
let res_body
const res_send = res.send
res.send = function(body)
{
res_body = body

res_send.call(res, body)
}

const 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 diff = process.hrtime(time);
const endOpts = {res: res, duration: diff[0] * 1e3 + diff[1] * 1e-6}

if (res_body !== undefined) {
endOpts.body = res_body;
}

log.info(endOpts, 'end request');
});

next();
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.3",
"bunyan": "^1.8.12",
"supertest": "^3.1.0",
"tape": "^4.9.1"
},
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
Expand Down