Skip to content

Commit

Permalink
Handle timestamp 0 in prettifyTime
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 committed Aug 12, 2019
1 parent a6b2032 commit d0a3f98
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/utils.js
Expand Up @@ -326,9 +326,19 @@ 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 `[${log}]`
}

0 comments on commit d0a3f98

Please sign in to comment.