Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add injection of lacking arguments of customPrettifiers #501

Merged
merged 4 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
})