Skip to content

Commit

Permalink
Support objects in options.customColors (#487)
Browse files Browse the repository at this point in the history
* Support objects in options.customColors

* Update error message

* Fix formatting

* Add unit test

---------

Co-authored-by: Chris Gonzalez <chris@gonzalez.capital>
  • Loading branch information
kindagonzo and Chris Gonzalez authored Mar 20, 2024
1 parent 711720a commit b7bf398
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 17 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,6 @@ profile-*

# Run Configuration
test/.tmp*

# Local History
.history
50 changes: 33 additions & 17 deletions lib/utils/parse-factory-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,39 @@ function parseFactoryOptions (options) {

let customColors
if (options.customColors) {
customColors = options.customColors.split(',').reduce((agg, value) => {
const [level, color] = value.split(':')

const condition = useOnlyCustomProps
? options.customLevels
: customLevelNames[level] !== undefined
const levelNum = condition
? customLevelNames[level]
: LEVEL_NAMES[level]
const colorIdx = levelNum !== undefined
? levelNum
: level

agg.push([colorIdx, color])

return agg
}, [])
if (typeof options.customColors === 'string') {
customColors = options.customColors.split(',').reduce((agg, value) => {
const [level, color] = value.split(':')
const condition = useOnlyCustomProps
? options.customLevels
: customLevelNames[level] !== undefined
const levelNum = condition
? customLevelNames[level]
: LEVEL_NAMES[level]
const colorIdx = levelNum !== undefined
? levelNum
: level
agg.push([colorIdx, color])
return agg
}, [])
} else if (typeof options.customColors === 'object') {
customColors = Object.keys(options.customColors).reduce((agg, value) => {
const [level, color] = [value, options.customColors[value]]
const condition = useOnlyCustomProps
? options.customLevels
: customLevelNames[level] !== undefined
const levelNum = condition
? customLevelNames[level]
: LEVEL_NAMES[level]
const colorIdx = levelNum !== undefined
? levelNum
: level
agg.push([colorIdx, color])
return agg
}, [])
} else {
throw new Error('options.customColors must be of type string or object.')
}
}

const customProperties = { customLevels, customLevelNames }
Expand Down
26 changes: 26 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1103,6 +1103,32 @@ test('basic prettifier tests', (t) => {
t.equal(formatted, `[${formattedEpoch}] INFO (${pid}): message {"extra":{"foo":"bar","number":42},"upper":"foobar"}\n`)
})

t.test('support custom colors object', async (t) => {
t.plan(1)
const pretty = prettyFactory({
colorize: true,
customColors: {
trace: 'cyan',
debug: 'blue',
info: 'green',
warn: 'yellow',
error: 'red',
fatal: 'red'
}
})
const log = pino({}, new Writable({
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
t.equal(
formatted,
`[${formattedEpoch}] \u001B[32mINFO\u001B[39m (${pid}): \u001B[36mfoo\u001B[39m\n`
)
cb()
}
}))
log.info('foo')
})

t.end()
})

Expand Down

0 comments on commit b7bf398

Please sign in to comment.