Skip to content

Commit

Permalink
New option and command line argument to ignore some keys (#57)
Browse files Browse the repository at this point in the history
* Add a new ignore options to the argument parser

* Filter log keys and ignore some of them

* New _ignore keys_ tests

* Add ignore documentation to the README

* Fix hardcoded test

* Fix a typo and update documentation
  • Loading branch information
maxme authored and jsumners committed Mar 30, 2019
1 parent f5dfdd3 commit 8fb8ca8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Readme.md
Expand Up @@ -66,8 +66,9 @@ letters see the [`dateformat` documentation](https://www.npmjs.com/package/datef
- Require a `SYS:` prefix to translate time to the local system's timezone. A
shortcut `SYS:standard` to translate time to `yyyy-mm-dd HH:MM:ss.l o` in
system timezone.
+ `--search` (`-s`): Specifiy a search pattern according to
+ `--search` (`-s`): Specify a search pattern according to
[jmespath](http://jmespath.org/).
+ `--ignore` (`-i`): Ignore one or several keys: (`-i time,hostname`)

<a id="integration"></a>
## Programmatic Integration
Expand Down Expand Up @@ -120,7 +121,8 @@ with keys corresponding to the options described in [CLI Arguments](#cliargs):
levelFirst: false, // --levelFirst
messageKey: 'msg', // --messageKey
translateTime: false, // --translateTime
search: 'foo == `bar`' // --search
search: 'foo == `bar`', // --search
ignore: 'pid,hostname' // --ignore
}
```

Expand Down
4 changes: 3 additions & 1 deletion bin.js
Expand Up @@ -16,7 +16,8 @@ args
.option(['k', 'errorLikeObjectKeys'], 'Define which keys contain error objects (`-k err,error`)', 'err,error')
.option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY)
.option(['t', 'translateTime'], 'Display epoch timestamps as UTC ISO format or according to an optional format string (default ISO 8601)')
.option(['s', 'search'], 'specifiy a search pattern according to jmespath')
.option(['s', 'search'], 'Specify a search pattern according to jmespath')
.option(['i', 'ignore'], 'Ignore one or several keys: (`-i time,hostname`)')

args
.example('cat log | pino-pretty', 'To prettify logs, simply pipe a log file through')
Expand All @@ -25,6 +26,7 @@ args
.example('cat log | pino-pretty -t "SYS:yyyy-mm-dd HH:MM:ss"', 'To convert Epoch timestamps to local timezone format use the -t option with "SYS:" prefixed format string')
.example('cat log | pino-pretty -l', 'To flip level and time/date in standard output use the -l option')
.example('cat log | pino-pretty -s "msg == \'hello world\'"', 'Only prints messages with msg equals to \'hello world\'')
.example('cat log | pino-pretty -i pid,hostname', 'Prettify logs but don\'t print pid and hostname')

const opts = args.parse(process.argv)
const pretty = prettyFactory(opts)
Expand Down
15 changes: 14 additions & 1 deletion index.js
Expand Up @@ -64,6 +64,7 @@ module.exports = function prettyFactory (options) {
const messageKey = opts.messageKey
const errorLikeObjectKeys = opts.errorLikeObjectKeys
const errorProps = opts.errorProps.split(',')
const ignoreKeys = opts.ignore ? new Set(opts.ignore.split(',')) : undefined

const color = {
default: nocolor,
Expand Down Expand Up @@ -108,6 +109,15 @@ module.exports = function prettyFactory (options) {
return
}

if (ignoreKeys) {
log = Object.keys(log)
.filter(key => !ignoreKeys.has(key))
.reduce((res, key) => {
res[key] = log[key]
return res
}, {})
}

const standardKeys = [
'pid',
'hostname',
Expand Down Expand Up @@ -149,7 +159,10 @@ module.exports = function prettyFactory (options) {
}

if (log.hostname) {
line += ' on ' + log.hostname
if (line.slice(-1) !== '(') {
line += ' '
}
line += 'on ' + log.hostname
}

line += ')'
Expand Down
14 changes: 14 additions & 0 deletions test/basic.test.js
Expand Up @@ -539,5 +539,19 @@ test('basic prettifier tests', (t) => {
})
})

t.test('ignores multiple keys', (t) => {
t.plan(1)
const pretty = prettyFactory({ ignore: 'pid,hostname' })
const arst = pretty(`{"msg":"hello world", "pid":"${pid}", "hostname":"${hostname}", "time":${epoch}, "level":30, "v":1}`)
t.is(arst, `[${epoch}] INFO : hello world\n`)
})

t.test('ignores a single key', (t) => {
t.plan(1)
const pretty = prettyFactory({ ignore: 'pid' })
const arst = pretty(`{"msg":"hello world", "pid":"${pid}", "hostname":"${hostname}", "time":${epoch}, "level":30, "v":1}`)
t.is(arst, `[${epoch}] INFO (on ${hostname}): hello world\n`)
})

t.end()
})
11 changes: 11 additions & 0 deletions test/cli.test.js
Expand Up @@ -53,5 +53,16 @@ test('cli', (t) => {
t.tearDown(() => child.kill())
})

t.test('does ignore multiple keys', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-i', 'pid,hostname'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[1522431328992] INFO : hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})

t.end()
})

0 comments on commit 8fb8ca8

Please sign in to comment.