Skip to content

Commit

Permalink
feat: child redact option
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed Jul 8, 2021
1 parent 78be33b commit 180de5f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
26 changes: 23 additions & 3 deletions lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ const {
serializersSym,
formattersSym,
useOnlyCustomLevelsSym,
needsMetadataGsym
needsMetadataGsym,
redactFmtSym,
stringifySym,
formatOptsSym,
stringifiersSym
} = require('./symbols')
const {
getLevel,
Expand All @@ -35,11 +39,13 @@ const {
const {
asChindings,
asJson,
buildFormatters
buildFormatters,
stringify
} = require('./tools')
const {
version
} = require('./meta')
const redaction = require('./redaction')

// note: use of class is satirical
// https://github.com/pinojs/pino/pull/433#pullrequestreview-127703127
Expand Down Expand Up @@ -71,10 +77,13 @@ module.exports = function () {
}

const resetChildingsFormatter = bindings => bindings
function child (bindings) {
// TODO: formatters, serializers, customLevels should move to options
// and depercation warning should emit
function child (bindings, options) {
if (!bindings) {
throw Error('missing bindings for child Pino')
}
options = options || {} // default options to empty object
const serializers = this[serializersSym]
const formatters = this[formattersSym]
const instance = Object.create(this)
Expand Down Expand Up @@ -119,6 +128,17 @@ function child (bindings) {
instance.levels = mappings(bindings.customLevels, instance[useOnlyCustomLevelsSym])
genLsCache(instance)
}

// redact must place before asChindings and only replace if exist
if ((typeof options.redact === 'object' && options.redact !== null) || Array.isArray(options.redact)) {
instance.redact = options.redact // replace redact directly
const stringifiers = redaction(instance.redact, stringify)
const formatOpts = { stringify: stringifiers[redactFmtSym] }
instance[stringifySym] = stringify
instance[stringifiersSym] = stringifiers
instance[formatOptsSym] = formatOpts
}

instance[chindingsSym] = asChindings(instance, bindings)
const childLevel = bindings.level || this.level
instance[setLevelSym](childLevel)
Expand Down
20 changes: 20 additions & 0 deletions test/redact.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -775,3 +775,23 @@ test('child bindings are redacted using wildcard and plain path keys', async ({
equal(req.headers.cookie, '[Redacted]')
equal(req.method, '[Redacted]')
})

test('child can customize redact', async ({ equal }) => {
const stream = sink()
const instance = pino({ redact: ['req.method', '*.headers.cookie'] }, stream)
instance.child({
req: {
method: 'GET',
url: '/',
headers: {
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
}
}
}, {
redact: ['req.url']
}).info('message completed')
const { req } = await once(stream, 'data')
equal(req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;')
equal(req.method, 'GET')
equal(req.url, '[Redacted]')
})

0 comments on commit 180de5f

Please sign in to comment.