Skip to content

Commit

Permalink
Prettify Time Before Filtering (#117)
Browse files Browse the repository at this point in the history
* Prettify Time Before Filtering

This allows the prettifiedTime function run before any of the properties
are filtered from the log object. If you are using a timestampKey that is
also ignored it will still work.

* Add Tests for Ignoring timestampKey

* Fix Lint Errors

* Add Factory Tests for Ignoring timestampKey
  • Loading branch information
BenGale authored Jul 11, 2020
1 parent 62a5f01 commit 38beec1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = function prettyFactory (options) {
}

const prettifiedMessage = prettifyMessage({ log, messageKey, colorizer, messageFormat })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey })

if (ignoreKeys) {
log = Object.keys(log)
Expand All @@ -86,7 +87,6 @@ module.exports = function prettyFactory (options) {

const prettifiedLevel = prettifyLevel({ log, colorizer, levelKey })
const prettifiedMetadata = prettifyMetadata({ log })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey })

let line = ''
if (opts.levelFirst && prettifiedLevel) {
Expand Down
14 changes: 14 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -643,5 +643,19 @@ test('basic prettifier tests', (t) => {
log.info('foo')
})

t.test('handles specified timestampKey', (t) => {
t.plan(1)
const pretty = prettyFactory({ timestampKey: '@timestamp' })
const arst = pretty(`{"msg":"hello world", "@timestamp":${epoch}, "level":30}`)
t.is(arst, `[${epoch}] INFO : hello world\n @timestamp: ${epoch}\n`)
})

t.test('handles using ignored timestampKey', (t) => {
t.plan(1)
const pretty = prettyFactory({ timestampKey: '@timestamp', ignore: '@timestamp' })
const arst = pretty(`{"msg":"hello world", "@timestamp":${epoch}, "level":30}`)
t.is(arst, `[${epoch}] INFO : hello world\n`)
})

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

t.test('uses specified timestampKey', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, '--timestampKey', '@timestamp'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), '[1522431328992] INFO : hello world\n @timestamp: 1522431328992\n')
})
const logLine = '{"level":30,"@timestamp":1522431328992,"msg":"hello world"}\n'
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})

t.test('uses an ignored timestampKey', (t) => {
t.plan(1)
const env = { TERM: 'dumb' }
const child = spawn(process.argv[0], [bin, '--timestampKey', '@timestamp', '--ignore', '@timestamp'], { env })
child.on('error', t.threw)
child.stdout.on('data', (data) => {
t.is(data.toString(), '[1522431328992] INFO : hello world\n')
})
const logLine = '{"level":30,"@timestamp":1522431328992,"msg":"hello world"}\n'
child.stdin.write(logLine)
t.tearDown(() => child.kill())
})

t.end()
})

0 comments on commit 38beec1

Please sign in to comment.