Skip to content

Commit

Permalink
Support redaction in pretty print
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Aug 8, 2018
1 parent 1b8cbdf commit 207abc7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/tools.js
Expand Up @@ -17,6 +17,7 @@ const {
stringifySym,
needsMetadataGsym,
wildcardGsym,
redactFmtSym,
streamSym
} = require('./symbols')

Expand Down Expand Up @@ -195,7 +196,10 @@ function prettifierMetaWrapper (pretty, dest) {
}
}

const formatted = pretty(obj)
const stringifiers = this.lastLogger[stringifiersSym]
const redact = stringifiers[redactFmtSym]

const formatted = pretty(typeof redact === 'function' ? redact(obj) : obj)
if (formatted === undefined) return
dest.write(formatted)
}
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/pretty/redact.js
@@ -0,0 +1,9 @@
global.process = { __proto__: process, pid: 123456 }
Date.now = function () { return 1459875739796 }
require('os').hostname = function () { return 'abcdefghijklmnopqr' }
var pino = require(require.resolve('./../../../'))
var log = pino({
prettyPrint: true,
redact: ['foo.an']
})
log.info({ foo: { an: 'object' } }, 'h')
14 changes: 14 additions & 0 deletions test/pretty.test.js
Expand Up @@ -121,3 +121,17 @@ test('applies serializers', async ({is, isNot}) => {
isNot(actual.match(/\(123456 on abcdefghijklmnopqr\): h/), null)
isNot(actual.match(/foo: "bar"/), null)
})

test('applies redaction rules', async ({is, isNot}) => {
var actual = ''
const child = fork(join(__dirname, 'fixtures', 'pretty', 'redact.js'), {silent: true})

child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
isNot(actual.match(/\(123456 on abcdefghijklmnopqr\): h/), null)
isNot(actual.match(/\[Redacted\]/), null)
is(actual.match(/object/), null)
})

0 comments on commit 207abc7

Please sign in to comment.