Skip to content

Commit

Permalink
Refactor prettify-time to use context
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Aug 30, 2023
1 parent 17c3bd3 commit 448253a
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 48 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ const pretty = require('./lib/pretty')
* lines.
* @property {string} [timestampKey='time'] Defines the key in incoming logs
* that contains the timestamp of the log, if present.
* @property {boolean} [translateTime=true] When true, will translate a
* JavaScript date integer into a human-readable string.
* @property {boolean|string} [translateTime=true] When true, will translate a
* JavaScript date integer into a human-readable string. If set to a string,
* it must be a format string.
* @property {boolean} [useOnlyCustomProps=true] When true, only custom levels
* and colors will be used if they have been provided.
*/
Expand Down
7 changes: 1 addition & 6 deletions lib/pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,7 @@ function pretty (inputData) {
}
})
const prettifiedMetadata = prettifyMetadata({ log, context: this.context })
const prettifiedTime = prettifyTime({
log,
translateFormat: this.translateTime,
timestampKey: this.timestampKey,
prettifier: this.customPrettifiers.time
})
const prettifiedTime = prettifyTime({ log, context: this.context })

let line = ''
if (this.levelFirst && prettifiedLevel) {
Expand Down
26 changes: 8 additions & 18 deletions lib/utils/prettify-time.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@

module.exports = prettifyTime

const {
TIMESTAMP_KEY
} = require('../constants')

const formatTime = require('./format-time')

/**
* @typedef {object} PrettifyTimeParams
* @property {object} log The log object with the timestamp to be prettified.
* @property {string} [timestampKey='time'] The log property that should be used
* to resolve timestamp value.
* @property {boolean|string} [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
* string to determine the output; see the `formatTime` function for details.
* @property {CustomPrettifierFunc} [prettifier] A user-supplied formatter
* for altering output.
* @property {PrettyContext} context The context object built from parsing
* the options.
*/

/**
Expand All @@ -31,12 +21,12 @@ const formatTime = require('./format-time')
* `undefined` is returned. Otherwise, the prettified time is returned as a
* string.
*/
function prettifyTime ({
log,
timestampKey = TIMESTAMP_KEY,
translateFormat = undefined,
prettifier
}) {
function prettifyTime ({ log, context }) {
const {
timestampKey,
translateTime: translateFormat
} = context
const prettifier = context.customPrettifiers?.time
let time = null

if (timestampKey in log) {
Expand Down
159 changes: 137 additions & 22 deletions lib/utils/prettify-time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,181 @@ process.env.TZ = 'UTC'

const tap = require('tap')
const prettifyTime = require('./prettify-time')
const {
TIMESTAMP_KEY
} = require('../constants')
const context = {
timestampKey: TIMESTAMP_KEY,
translateTime: true,
customPrettifiers: {}
}

tap.test('returns `undefined` if `time` or `timestamp` not in log', async t => {
const str = prettifyTime({ log: {} })
const str = prettifyTime({ log: {}, context })
t.equal(str, undefined)
})

tap.test('returns prettified formatted time from custom field', async t => {
const log = { customtime: 1554642900000 }
let str = prettifyTime({ log, translateFormat: true, timestampKey: 'customtime' })
let str = prettifyTime({
log,
context: {
...context,
timestampKey: 'customtime'
}
})
t.equal(str, '[13:15:00.000]')

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

tap.test('returns prettified formatted time', async t => {
let log = { time: 1554642900000 }
let str = prettifyTime({ log, translateFormat: true })
let str = prettifyTime({
log,
context: {
...context
}
})
t.equal(str, '[13:15:00.000]')

log = { timestamp: 1554642900000 }
str = prettifyTime({ log, translateFormat: true })
str = prettifyTime({
log,
context: {
...context
}
})
t.equal(str, '[13:15:00.000]')

log = { time: '2019-04-07T09:15:00.000-04:00' }
str = prettifyTime({ log, translateFormat: true })
str = prettifyTime({
log,
context: {
...context
}
})
t.equal(str, '[13:15:00.000]')

log = { timestamp: '2019-04-07T09:15:00.000-04:00' }
str = prettifyTime({ log, translateFormat: true })
str = prettifyTime({
log,
context: {
...context
}
})
t.equal(str, '[13:15:00.000]')

log = { time: 1554642900000 }
str = prettifyTime({ log, translateFormat: 'd mmm yyyy H:MM' })
str = prettifyTime({
log,
context: {
...context,
translateTime: 'd mmm yyyy H:MM'
}
})
t.equal(str, '[7 Apr 2019 13:15]')

log = { timestamp: 1554642900000 }
str = prettifyTime({ log, translateFormat: 'd mmm yyyy H:MM' })
str = prettifyTime({
log,
context: {
...context,
translateTime: 'd mmm yyyy H:MM'
}
})
t.equal(str, '[7 Apr 2019 13:15]')

log = { time: '2019-04-07T09:15:00.000-04:00' }
str = prettifyTime({ log, translateFormat: 'd mmm yyyy H:MM' })
str = prettifyTime({
log,
context: {
...context,
translateTime: 'd mmm yyyy H:MM'
}
})
t.equal(str, '[7 Apr 2019 13:15]')

log = { timestamp: '2019-04-07T09:15:00.000-04:00' }
str = prettifyTime({ log, translateFormat: 'd mmm yyyy H:MM' })
str = prettifyTime({
log,
context: {
...context,
translateTime: 'd mmm yyyy H:MM'
}
})
t.equal(str, '[7 Apr 2019 13:15]')
})

tap.test('passes through value', async t => {
let log = { time: 1554642900000 }
let str = prettifyTime({ log })
let str = prettifyTime({
log,
context: {
...context,
translateTime: undefined
}
})
t.equal(str, '[1554642900000]')

log = { timestamp: 1554642900000 }
str = prettifyTime({ log })
str = prettifyTime({
log,
context: {
...context,
translateTime: undefined
}
})
t.equal(str, '[1554642900000]')

log = { time: '2019-04-07T09:15:00.000-04:00' }
str = prettifyTime({ log })
str = prettifyTime({
log,
context: {
...context,
translateTime: undefined
}
})
t.equal(str, '[2019-04-07T09:15:00.000-04:00]')

log = { timestamp: '2019-04-07T09:15:00.000-04:00' }
str = prettifyTime({ log })
str = prettifyTime({
log,
context: {
...context,
translateTime: undefined
}
})
t.equal(str, '[2019-04-07T09:15:00.000-04:00]')
})

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

log = { timestamp: 0 }
str = prettifyTime({ log })
str = prettifyTime({
log,
context: {
...context,
translateTime: undefined
}
})
t.equal(str, '[0]')
})

Expand All @@ -86,15 +187,24 @@ tap.test('works with epoch as a number or string', (t) => {
const epoch = 1522431328992
const asNumber = prettifyTime({
log: { time: epoch, msg: 'foo' },
translateFormat: true
context: {
...context,
translateTime: true
}
})
const asString = prettifyTime({
log: { time: `${epoch}`, msg: 'foo' },
translateFormat: true
context: {
...context,
translateTime: true
}
})
const invalid = prettifyTime({
log: { time: '2 days ago', msg: 'foo' },
translateFormat: true
context: {
...context,
translateTime: true
}
})
t.same(asString, '[17:35:28.992]')
t.same(asNumber, '[17:35:28.992]')
Expand All @@ -104,8 +214,13 @@ tap.test('works with epoch as a number or string', (t) => {
tap.test('uses custom prettifier', async t => {
const str = prettifyTime({
log: { time: 0 },
prettifier () {
return 'done'
context: {
...context,
customPrettifiers: {
time () {
return 'done'
}
}
}
})
t.equal(str, 'done')
Expand Down

0 comments on commit 448253a

Please sign in to comment.