Skip to content

Commit

Permalink
Merge pull request #225 from kapalex/master
Browse files Browse the repository at this point in the history
#66 add response and request whitelist nesting
  • Loading branch information
bithavoc authored Sep 26, 2019
2 parents ce56eaf + aebd898 commit 5b06988
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 4 deletions.
35 changes: 35 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,40 @@ Note that you can log the whole request and/or response body:

expressWinston.requestWhitelist.push('body');
expressWinston.responseWhitelist.push('body');

### Nested Whitelists

`requestWhitelist` and `responseWhitelist` also support nested whitelist values, allowing access to parts of an object.

For example, using the following during logger setup:

expressWinston.responseWhitelist.push('body.import.value');

A response that looks like this :

{
body: {
important: {
value: 5
},
notImportant: {
value: 7
}
},
other: {
value: 3
}
}

Would only log the following value :

{
body: {
important: {
value: 5
}
}
}

## Route-Specific Whitelists and Blacklists

Expand Down Expand Up @@ -456,6 +490,7 @@ If you ran into any problems, please use the project [Issues section](https://gi
* [Lars Jacob](https://github.com/jaclar) (https://github.com/jaclar)
* [Jonathan Lomas](https://github.com/floatingLomas) (https://github.com/floatingLomas)
* [Ross Brandes](https://github.com/rosston) (https://github.com/rosston)
* [Alex Kaplan](https://github.com/kapalex) (https://github.com/kapalex)

Also see AUTHORS file, add yourself if you are missing.

Expand Down
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ exports.ignoredRoutes = [];
* @return {*}
*/
exports.defaultRequestFilter = function (req, propName) {
return req[propName];
return _.get(req, propName);
};

/**
Expand All @@ -82,7 +82,7 @@ exports.defaultHeaderBlacklist = [];
* @return {*}
*/
exports.defaultResponseFilter = function (res, propName) {
return res[propName];
return _.get(res, propName);
};

/**
Expand All @@ -102,7 +102,7 @@ function filterObject(originalObj, whiteList, headerBlacklist, initialFilter) {
var value = initialFilter(originalObj, propName);

if(typeof (value) !== 'undefined') {
obj[propName] = value;
_.set(obj, propName, value);
fieldsSet = true;
if(propName === 'headers') {
[].concat(headerBlacklist).forEach(function (headerName) {
Expand Down Expand Up @@ -304,7 +304,7 @@ exports.logger = function logger(options) {

logData.res = res;

if (_.includes(responseWhitelist, 'body')) {
if (_.includes(responseWhitelist.map(term => term.split('.')[0]), 'body')) {
if (chunk) {
var isJson = (res.getHeader('content-type')
&& res.getHeader('content-type').indexOf('json') >= 0);
Expand Down
26 changes: 26 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,19 @@ describe('express-winston', function () {
result.log.meta.req.should.not.have.property('method');
});
});

it('should work with nested requestWhitelist', function () {
var options = {
req: {foo: {test: "bar"}},
loggerOptions: {
requestWhitelist: ['foo.test']
}
};
return errorLoggerTestHelper(options).then(function (result) {
result.log.meta.req.should.have.property('foo');
result.log.meta.req.foo.should.have.property('test');
});
});
});

describe('dynamicMeta option', function () {
Expand Down Expand Up @@ -1283,6 +1296,19 @@ describe('express-winston', function () {
result.log.meta.res.should.not.have.property('method');
});
});

it('should work with nested responseWhitelist', function () {
var options = {
res: {foo: {test: "bar"}},
loggerOptions: {
responseWhitelist: ['foo.test']
}
};
return loggerTestHelper(options).then(function (result) {
result.log.meta.res.should.have.property('foo');
result.log.meta.res.foo.should.have.property('test');
});
});
});

describe('ignoredRoutes option', function () {
Expand Down

0 comments on commit 5b06988

Please sign in to comment.