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

Add new option to fallback custom levels and colors to default values #317

Merged
merged 5 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ args
.option(['e', 'errorProps'], 'Comma separated list of properties on error objects to show (`*` for all properties) (defaults to ``)')
.option(['l', 'levelFirst'], 'Display the log level as the first output field')
.option(['L', 'minimumLevel'], 'Hide messages below the specified log level')
.option(['x', 'customLevels'], 'Override default levels (`-x err:99,info:1`)')
.option(['X', 'customColors'], 'Override default colors using names from https://www.npmjs.com/package/colorette (`-X err:red,info:blue`)')
.option(['x', 'customLevels'], 'Add custom levels (`-x err:99,info:1`)')
.option(['X', 'customColors'], 'Add custom colors using names from https://www.npmjs.com/package/colorette (`-X err:red,info:blue`)')
.option(['k', 'errorLikeObjectKeys'], 'Define which keys contain error objects (`-k err,error`) (defaults to `err,error`)')
.option(['m', 'messageKey'], 'Highlight the message under the specified key', CONSTANTS.MESSAGE_KEY)
.option('levelKey', 'Detect the log level under the specified key', CONSTANTS.LEVEL_KEY)
Expand Down
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,32 @@ function prettyFactory (options) {

return agg
}, { default: 'USERLVL' })
: undefined
: { default: 'USERLVL' }
const customLevelNames = opts.customLevels
? opts.customLevels
.split(',')
.reduce((agg, value, idx) => {
const [levelName, levelIdx = idx] = value.split(':')

agg[levelName] = levelIdx
agg[levelName.toLowerCase()] = levelIdx

return agg
}, {})
: undefined
: {}
const customColors = opts.customColors
? opts.customColors
.split(',')
.reduce((agg, value) => {
const [level, color] = value.split(':')

const levelNum = customLevelNames !== undefined ? customLevelNames[level] : LEVEL_NAMES[level]
const levelNum = customLevelNames[level] !== undefined ? customLevelNames[level] : LEVEL_NAMES[level]
const colorIdx = levelNum !== undefined ? levelNum : level

agg.push([colorIdx, color])

return agg
}, [])
: undefined
: []
const customPrettifiers = opts.customPrettifiers
const ignoreKeys = opts.ignore ? new Set(opts.ignore.split(',')) : undefined
const hideObject = opts.hideObject
Expand All @@ -116,7 +116,7 @@ function prettyFactory (options) {
}

if (minimumLevel) {
const minimum = (customLevelNames === undefined ? LEVEL_NAMES[minimumLevel] : customLevelNames[minimumLevel]) || Number(minimumLevel)
const minimum = (customLevelNames[minimumLevel] === undefined ? LEVEL_NAMES[minimumLevel] : customLevelNames[minimumLevel]) || Number(minimumLevel)
const level = log[levelKey === undefined ? LEVEL_KEY : levelKey]
if (level < minimum) return
}
Expand Down
9 changes: 5 additions & 4 deletions lib/colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function resolveCustomColoredColorizer (customColors) {
}

function colorizeLevel (level, colorizer, { customLevels, customLevelNames } = {}) {
const levels = customLevels || LEVELS
const levelNames = customLevelNames || LEVEL_NAMES
const levels = Object.assign({}, LEVELS, customLevels || {})
const levelNames = Object.assign({}, LEVEL_NAMES, customLevelNames || {})

let levelNum = 'default'
if (Number.isInteger(+level)) {
Expand All @@ -71,7 +71,8 @@ coloredColorizer.message = colored.message
coloredColorizer.greyMessage = colored.greyMessage

function customColoredColorizerFactory (customColors) {
const customColored = resolveCustomColoredColorizer(customColors)
const customOnlyColored = resolveCustomColoredColorizer(customColors)
const customColored = Object.assign({}, colored, customOnlyColored)

const customColoredColorizer = function (level, opts) {
return colorizeLevel(level, customColored, opts)
Expand All @@ -98,7 +99,7 @@ function customColoredColorizerFactory (customColors) {
* string is not a recognized level name.
*/
module.exports = function getColorizer (useColors = false, customColors) {
if (useColors && customColors !== undefined) {
if (useColors && customColors !== undefined && customColors.length > 0) {
return customColoredColorizerFactory(customColors)
} else if (useColors) {
return coloredColorizer
Expand Down
4 changes: 2 additions & 2 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,12 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE
* key is not a string, then `undefined` will be returned. Otherwise, a string
* that is the prettified message.
*/
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL, levelKey = LEVEL_KEY, customLevels }) {
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL, levelKey = LEVEL_KEY, customLevels = { default: 'USERLVL' } }) {
if (messageFormat && typeof messageFormat === 'string') {
const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) {
// return log level as string instead of int
if (p1 === levelLabel && log[levelKey]) {
return customLevels === undefined ? LEVELS[log[levelKey]] : customLevels[log[levelKey]]
return customLevels[log[levelKey]] === undefined ? LEVELS[log[levelKey]] : customLevels[log[levelKey]]
}
// Parse nested key access, e.g. `{keyA.subKeyB}`.
return p1.split('.').reduce(function (prev, curr) {
Expand Down
6 changes: 4 additions & 2 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,12 @@ test('basic prettifier tests', (t) => {
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
const localHour = dateformat(epoch, 'HH')
const localMinute = dateformat(epoch, 'MM')
const localDate = dateformat(epoch, 'yyyy-mm-dd')
const offset = dateformat(epoch, 'o')
t.equal(
formatted,
`[${localDate} ${localHour}:35:28.992 ${offset}] INFO (${pid} on ${hostname}): foo\n`
`[${localDate} ${localHour}:${localMinute}:28.992 ${offset}] INFO (${pid} on ${hostname}): foo\n`
)
cb()
}
Expand All @@ -329,11 +330,12 @@ test('basic prettifier tests', (t) => {
write (chunk, enc, cb) {
const formatted = pretty(chunk.toString())
const localHour = dateformat(epoch, 'HH')
const localMinute = dateformat(epoch, 'MM')
const localDate = dateformat(epoch, 'yyyy/mm/dd')
const offset = dateformat(epoch, 'o')
t.equal(
formatted,
`[${localDate} ${localHour}:35:28 ${offset}] INFO (${pid} on ${hostname}): foo\n`
`[${localDate} ${localHour}:${localMinute}:28 ${offset}] INFO (${pid} on ${hostname}): foo\n`
)
cb()
}
Expand Down
3 changes: 3 additions & 0 deletions test/lib/colors.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ const testCustomColoringColorizer = getColorizer => async t => {

colorized = colorizer('use-default')
t.equal(colorized, '\u001B[37mUSERLVL\u001B[39m')

colorized = colorizer(40, opts)
t.equal(colorized, '\u001B[33mWARN\u001B[39m')
}

test('returns default colorizer - private export', testDefaultColorizer(getColorizerPrivate))
Expand Down