Skip to content

Commit

Permalink
Merge 409fbaf into 78e000f
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 14, 2018
2 parents 78e000f + 409fbaf commit 99a2f97
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ Default: `msg`.
use the system timezone for displaying the time.
+ `--translateTime` (`-t`): Translate the epoch time value into a human readable
date and time string. See `--dateFormat` for information on the output format.
+ `--search` (`-s`): Specifiy a search pattern according to
[jmespath](http://jmespath.org/).

<a id="api"></a>
## API
Expand All @@ -76,6 +78,7 @@ in [CLI Arguments](#cliargs):
localTime: false, // --localTime
messageKey: 'msg', // --messageKey
translateTime: false // --translateTime
search: 'foo == `bar`' // --search
}
```

Expand Down
2 changes: 2 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ args
.option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY)
.option(['n', 'localTime'], 'Display timestamps according to system timezone')
.option(['t', 'translateTime'], 'Convert Epoch timestamps to ISO format')
.option(['s', 'search'], 'specifiy a search pattern according to jmespath')

args
.example('cat log | pino-pretty', 'To prettify logs, simply pipe a log file through')
.example('cat log | pino-pretty -m fooMessage', 'To highlight a string at a key other than \'msg\', use')
.example('cat log | pino-pretty -t', 'To convert Epoch timestamps to ISO timestamps use the -t option')
.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\'')

const opts = args.parse(process.argv)
const pretty = prettyFactory(opts)
Expand Down
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

const chalk = require('chalk')
const dateformat = require('dateformat')
// remove jsonParser once Node 6 is not supported anymore
const jsonParser = require('fast-json-parse')
const jmespath = require('jmespath')

const CONSTANTS = require('./lib/constants')

Expand Down Expand Up @@ -84,6 +86,8 @@ module.exports = function prettyFactory (options) {

pretty.asMetaWrapper = asMetaWrapper

const search = opts.search

return pretty

function pretty (inputData) {
Expand All @@ -99,6 +103,10 @@ module.exports = function prettyFactory (options) {
log = inputData
}

if (search && !jmespath.search(log, search)) {
return
}

const standardKeys = [
'pid',
'hostname',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"chalk": "^2.3.2",
"dateformat": "^3.0.3",
"fast-json-parse": "^1.0.3",
"jmespath": "^0.15.0",
"pump": "^3.0.0",
"split2": "^2.2.0",
"through2": "^2.0.3"
Expand Down
24 changes: 24 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,5 +375,29 @@ test('basic prettifier tests', (t) => {
log.info('foo')
})

t.test('filter some lines based on jmespath', (t) => {
t.plan(3)
const pretty = prettyFactory({ search: 'foo.bar' })
const expected = [
undefined,
undefined,
`[${epoch}] INFO (${pid} on ${hostname}): foo\n foo: {\n "bar": true\n }\n`
]
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.is(
formatted,
expected.shift()
)
cb()
}
}))
log.info('foo')
log.info({ something: 'else' }, 'foo')
// only this line will be formatted
log.info({ foo: { bar: true } }, 'foo')
})

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

t.test('does search', (t) => {
t.plan(1)
const child = spawn(process.argv0, [bin, '-s', 'msg == `hello world`'])
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), `[${epoch}] INFO (42 on foo): hello world\n`)
})
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})

t.end()
})

0 comments on commit 99a2f97

Please sign in to comment.