Skip to content

Commit

Permalink
Support custom timestamp property (#67)
Browse files Browse the repository at this point in the history
* Add support for custom timestamp key

* Add missing documentation entry
  • Loading branch information
kibertoad authored and jsumners committed May 25, 2019
1 parent 9a5e4d8 commit 3fa5047
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 8 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Expand Up @@ -62,6 +62,8 @@ pino app.js | pino-pretty
error like objects. Default: `err,error`.
- `--messageKey` (`-m`): Define the key that contains the main log message.
Default: `msg`.
- `--timestampKey` (`-m`): Define the key that contains the log timestamp.
Default: `time`.
- `--translateTime` (`-t`): Translate the epoch time value into a human readable
date and time string. This flag also can set the format string to apply when
translating the date to human readable format. For a list of available pattern
Expand Down Expand Up @@ -124,6 +126,7 @@ with keys corresponding to the options described in [CLI Arguments](#cliargs):
errorProps: '', // --errorProps
levelFirst: false, // --levelFirst
messageKey: 'msg', // --messageKey
timestampKey: 'time', // --timestampKey
translateTime: false, // --translateTime
search: 'foo == `bar`', // --search
ignore: 'pid,hostname' // --ignore
Expand Down
2 changes: 2 additions & 0 deletions bin.js
Expand Up @@ -15,13 +15,15 @@ args
.option(['l', 'levelFirst'], 'Display the log level as the first output field')
.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(['a', 'timestampKey'], 'Display the timestamp from the specified key', CONSTANTS.TIMESTAMP_KEY)
.option(['t', 'translateTime'], 'Display epoch timestamps as UTC ISO format or according to an optional format string (default ISO 8601)')
.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')
.example('cat log | pino-pretty -m fooMessage', 'To highlight a string at a key other than \'msg\', use')
.example('cat log | pino-pretty -a fooTimestamp', 'To display timestamp from a key other than \'time\', use')
.example('cat log | pino-pretty -t', 'To convert Epoch timestamps to ISO timestamps use the -t option')
.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')
Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -3,7 +3,7 @@
const chalk = require('chalk')
const jmespath = require('jmespath')
const colors = require('./lib/colors')
const { ERROR_LIKE_KEYS, MESSAGE_KEY } = require('./lib/constants')
const { ERROR_LIKE_KEYS, MESSAGE_KEY, TIMESTAMP_KEY } = require('./lib/constants')
const {
isObject,
prettifyErrorLog,
Expand All @@ -30,6 +30,7 @@ const defaultOptions = {
errorProps: '',
levelFirst: false,
messageKey: MESSAGE_KEY,
timestampKey: TIMESTAMP_KEY,
translateTime: false,
useMetadata: false,
outputStream: process.stdout
Expand All @@ -40,6 +41,7 @@ module.exports = function prettyFactory (options) {
const EOL = opts.crlf ? '\r\n' : '\n'
const IDENT = ' '
const messageKey = opts.messageKey
const timestampKey = opts.timestampKey
const errorLikeObjectKeys = opts.errorLikeObjectKeys
const errorProps = opts.errorProps.split(',')
const ignoreKeys = opts.ignore ? new Set(opts.ignore.split(',')) : undefined
Expand Down Expand Up @@ -83,7 +85,7 @@ module.exports = function prettyFactory (options) {
const prettifiedLevel = prettifyLevel({ log, colorizer })
const prettifiedMessage = prettifyMessage({ log, messageKey, colorizer })
const prettifiedMetadata = prettifyMetadata({ log })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey })

let line = ''
if (opts.levelFirst && prettifiedLevel) {
Expand Down
2 changes: 2 additions & 0 deletions lib/constants.js
Expand Up @@ -7,6 +7,8 @@ module.exports = {

MESSAGE_KEY: 'msg',

TIMESTAMP_KEY: 'time',

LEVELS: {
default: 'USERLVL',
60: 'FATAL',
Expand Down
14 changes: 8 additions & 6 deletions lib/utils.js
Expand Up @@ -7,6 +7,7 @@ const {
DATE_FORMAT,
ERROR_LIKE_KEYS,
MESSAGE_KEY,
TIMESTAMP_KEY,
LOGGER_KEYS
} = require('./constants')

Expand Down Expand Up @@ -309,11 +310,12 @@ function prettifyObject ({
}

/**
* Prettifies a timestamp if the given `log` has either `time` or `timestamp`
* properties.
* Prettifies a timestamp if the given `log` has either `time`, `timestamp` or custom specified timestamp
* property.
*
* @param {object} input
* @param {object} input.log The log object with the timestamp to be prettified.
* @param {string} [input.timestampKey='time'] The log property that should be used to resolve timestamp value
* @param {bool|string} [input.translateFormat=undefined] When `true` the
* timestamp will be prettified into a string at UTC using the default
* `DATE_FORMAT`. If a string, then `translateFormat` will be used as the format
Expand All @@ -323,10 +325,10 @@ function prettifyObject ({
* `undefined` is returned. Otherwise, the prettified time is returned as a
* string.
*/
function prettifyTime ({ log, translateFormat = undefined }) {
if ('time' in log === false && 'timestamp' in log === false) return undefined
function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined }) {
if (timestampKey in log === false && 'timestamp' in log === false) return undefined
if (translateFormat) {
return '[' + formatTime(log.time || log.timestamp, translateFormat) + ']'
return '[' + formatTime(log[timestampKey] || log.timestamp, translateFormat) + ']'
}
return `[${log.time || log.timestamp}]`
return `[${log[timestampKey] || log.timestamp}]`
}
9 changes: 9 additions & 0 deletions test/lib/utils.public.test.js
Expand Up @@ -191,6 +191,15 @@ tap.test('prettifyTime', t => {
t.is(str, undefined)
})

t.test('returns prettified formatted time from custom field', async t => {
let log = { customtime: 1554642900000 }
let str = prettifyTime({ log, translateFormat: true, timestampKey: 'customtime' })
t.is(str, '[2019-04-07 13:15:00.000 +0000]')

str = prettifyTime({ log, translateFormat: false, timestampKey: 'customtime' })
t.is(str, '[1554642900000]')
})

t.test('returns prettified formatted time', async t => {
let log = { time: 1554642900000 }
let str = prettifyTime({ log, translateFormat: true })
Expand Down

0 comments on commit 3fa5047

Please sign in to comment.