Skip to content

Commit

Permalink
fix: add injection of lacking arguments of customPrettifiers (#501)
Browse files Browse the repository at this point in the history
* fix: add injection lacking arguments of customPrettifiers

* test: add unit test for log metadata colorizing

* refactor: fix import order in test

* refactor: add underscores to unused function arguments
  • Loading branch information
pasha-vuiko committed Jun 9, 2024
1 parent 77e85fd commit 84680aa
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
22 changes: 17 additions & 5 deletions lib/utils/prettify-metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ module.exports = prettifyMetadata
* returned. Otherwise, a string of prettified metadata is returned.
*/
function prettifyMetadata ({ log, context }) {
const prettifiers = context.customPrettifiers
const { customPrettifiers: prettifiers, colorizer } = context
let line = ''

if (log.name || log.pid || log.hostname) {
line += '('

if (log.name) {
line += prettifiers.name ? prettifiers.name(log.name) : log.name
line += prettifiers.name
? prettifiers.name(log.name, 'name', log, { colors: colorizer.colors })
: log.name
}

if (log.pid) {
const prettyPid = prettifiers.pid ? prettifiers.pid(log.pid) : log.pid
const prettyPid = prettifiers.pid
? prettifiers.pid(log.pid, 'pid', log, { colors: colorizer.colors })
: log.pid
if (log.name && log.pid) {
line += '/' + prettyPid
} else {
Expand All @@ -43,14 +47,22 @@ function prettifyMetadata ({ log, context }) {
if (log.hostname) {
// If `pid` and `name` were in the ignore keys list then we don't need
// the leading space.
line += `${line === '(' ? 'on' : ' on'} ${prettifiers.hostname ? prettifiers.hostname(log.hostname) : log.hostname}`
const prettyHostname = prettifiers.hostname
? prettifiers.hostname(log.hostname, 'hostname', log, { colors: colorizer.colors })
: log.hostname

line += `${line === '(' ? 'on' : ' on'} ${prettyHostname}`
}

line += ')'
}

if (log.caller) {
line += `${line === '' ? '' : ' '}<${prettifiers.caller ? prettifiers.caller(log.caller) : log.caller}>`
const prettyCaller = prettifiers.caller
? prettifiers.caller(log.caller, 'caller', log, { colors: colorizer.colors })
: log.caller

line += `${line === '' ? '' : ' '}<${prettyCaller}>`
}

if (line === '') {
Expand Down
42 changes: 40 additions & 2 deletions lib/utils/prettify-metadata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

const tap = require('tap')
const prettifyMetadata = require('./prettify-metadata')
const getColorizer = require('../colors')
const context = {
customPrettifiers: {}
customPrettifiers: {},
colorizer: {
colors: {}
}
}

tap.test('returns `undefined` if no metadata present', async t => {
Expand Down Expand Up @@ -104,8 +108,42 @@ tap.test('uses prettifiers from passed prettifiers object', async t => {
const str = prettifyMetadata({
log: { pid: '1234', hostname: 'bar', caller: 'baz', name: 'joe' },
context: {
customPrettifiers: prettifiers
customPrettifiers: prettifiers,
colorizer: { colors: {} }
}
})
t.equal(str, '(JOE/1234__ on BAR) <BAZ>')
})

tap.test('uses colorizer from passed context to colorize metadata', async t => {
const prettifiers = {
name (input, _key, _log, { colors }) {
return colors.blue(input)
},
pid (input, _key, _log, { colors }) {
return colors.red(input)
},
hostname (input, _key, _log, { colors }) {
return colors.green(input)
},
caller (input, _key, _log, { colors }) {
return colors.cyan(input)
}
}
const log = { name: 'foo', pid: '1234', hostname: 'bar', caller: 'baz' }
const colorizer = getColorizer(true)
const context = {
customPrettifiers: prettifiers,
colorizer
}

const result = prettifyMetadata({ log, context })

const colorizedName = colorizer.colors.blue(log.name)
const colorizedPid = colorizer.colors.red(log.pid)
const colorizedHostname = colorizer.colors.green(log.hostname)
const colorizedCaller = colorizer.colors.cyan(log.caller)
const expected = `(${colorizedName}/${colorizedPid} on ${colorizedHostname}) <${colorizedCaller}>`

t.equal(result, expected)
})

0 comments on commit 84680aa

Please sign in to comment.