Skip to content

Commit

Permalink
Handle timestamp 0 in prettifyTime (#76)
Browse files Browse the repository at this point in the history
When trying to format a log object like the following:

```json
{
  "level": 30,
  "time": 0,
  "name": "foobar",
  "msg": "I like cake",
  "v": 1
}
```

pino-pretty previously tried to format the timestamp `undefined` instead of
taking the `0` time. This commit looks not at the truthiness of the value, but
its existence.
  • Loading branch information
Zirak authored and mcollina committed Aug 15, 2019
1 parent a6b2032 commit 7f19af0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/utils.js
Expand Up @@ -326,9 +326,18 @@ function prettifyObject ({
* string.
*/
function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined }) {
if (timestampKey in log === false && 'timestamp' in log === false) return undefined
let time = null

if (timestampKey in log) {
time = log[timestampKey]
} else if ('timestamp' in log) {
time = log.timestamp
}

if (time === null) return undefined
if (translateFormat) {
return '[' + formatTime(log[timestampKey] || log.timestamp, translateFormat) + ']'
return '[' + formatTime(time, translateFormat) + ']'
}
return `[${log[timestampKey] || log.timestamp}]`

return `[${time}]`
}
10 changes: 10 additions & 0 deletions test/lib/utils.public.test.js
Expand Up @@ -252,5 +252,15 @@ tap.test('prettifyTime', t => {
t.is(str, '[2019-04-07T09:15:00.000-04:00]')
})

t.test('handles the 0 timestamp', async t => {
let log = { time: 0 }
let str = prettifyTime({ log })
t.is(str, '[0]')

log = { timestamp: 0 }
str = prettifyTime({ log })
t.is(str, '[0]')
})

t.end()
})

0 comments on commit 7f19af0

Please sign in to comment.