diff --git a/src/util/common/logForLevel.js b/src/util/common/logForLevel.js index 3e00a1f..1b5b572 100644 --- a/src/util/common/logForLevel.js +++ b/src/util/common/logForLevel.js @@ -15,6 +15,6 @@ export default function logForLevel(level) { * @return {undefined} */ return function logIt(...args) { - return this._logger[level].apply(this._logger, scrub(args, this._config)); + return this._logger[level].apply(this._logger, scrub(args, this._config, this._logger)); } } diff --git a/src/util/common/scrub.js b/src/util/common/scrub.js index cce6790..1f2a8bb 100644 --- a/src/util/common/scrub.js +++ b/src/util/common/scrub.js @@ -1,8 +1,31 @@ +import isObject from 'lodash/isObject'; +import tail from 'lodash/tail'; +import mapValues from 'lodash/mapValues'; import hideSecrets from 'hide-secrets'; -export default function scrub(args, config = {}) { +function getScrubConfig(config) { + return { badWords: config.scrubFields }; +} + +function handleContext(data, config = {}, logger) { + const serializedFields = Object.keys(logger.serializers || {}); + + return mapValues(data, (val, key) => { + if (serializedFields.includes(key)) { + return val; + } + + return hideSecrets(val, getScrubConfig(config)); + }); +} + +export default function scrub(args, config = {}, logger) { if (Array.isArray(config.scrubFields) && config.scrubFields.length) { - return hideSecrets(args, { badWords: config.scrubFields }); + if (isObject(args[0])) { + return [handleContext(args[0], config, logger), ...hideSecrets(tail(args), getScrubConfig(config))] + } + + return hideSecrets(args, getScrubConfig(config)); } return args; diff --git a/test/specs/logger.spec.js b/test/specs/logger.spec.js index fcbe106..d6ddaf3 100644 --- a/test/specs/logger.spec.js +++ b/test/specs/logger.spec.js @@ -113,10 +113,10 @@ describe('we-js-logger', () => { }); it('hides secrets for fields', () => { - log.info({ scrubMe: 'This should be ignored', tlc: 'No Scrubs' }); + log.info({ data: { scrubMe: 'This should be ignored', tlc: 'No Scrubs' } }); expect(log._logger._emit).to.have.been.calledOnce; - expect(log._logger._emit.firstCall.args[0].scrubMe).to.equal('[SECRET]'); - expect(log._logger._emit.firstCall.args[0].tlc).to.equal('No Scrubs'); + expect(log._logger._emit.firstCall.args[0].data.scrubMe).to.equal('[SECRET]'); + expect(log._logger._emit.firstCall.args[0].data.tlc).to.equal('No Scrubs'); }); it('hides secrets for msg', () => { @@ -134,10 +134,10 @@ describe('we-js-logger', () => { }); it('hides secrets for fields', () => { - childLog.info({ scrubMe: 'This should be ignored', tlc: 'No Scrubs' }); + childLog.info({ data: { scrubMe: 'This should be ignored', tlc: 'No Scrubs' } }); expect(childLog._logger._emit).to.have.been.calledOnce; - expect(childLog._logger._emit.firstCall.args[0].scrubMe).to.equal('[SECRET]'); - expect(childLog._logger._emit.firstCall.args[0].tlc).to.equal('No Scrubs'); + expect(childLog._logger._emit.firstCall.args[0].data.scrubMe).to.equal('[SECRET]'); + expect(childLog._logger._emit.firstCall.args[0].data.tlc).to.equal('No Scrubs'); }); it('hides secrets for msg', () => { diff --git a/test/specs/util/scrub.spec.js b/test/specs/util/scrub.spec.js index 60c4652..cbbd78f 100644 --- a/test/specs/util/scrub.spec.js +++ b/test/specs/util/scrub.spec.js @@ -3,6 +3,8 @@ import scrub from '../../../src/util/common/scrub'; // FIXME stub out `hideSecrets` -- doesn't seem to be working on first pass // import * as hideSecretsModule from 'hide-secrets'; +// TODO test special `handleContext` behavior + describe('util/common/scrub', () => { let args; beforeEach(() => {