diff --git a/lib/colors.js b/lib/colors.js index bfa5ded5..6d74a44a 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -31,7 +31,7 @@ const colored = { greyMessage: gray } -function resolveCustomColored (customColors) { +function resolveCustomColoredColorizer (customColors) { return customColors.reduce( function (agg, [level, color]) { agg[level] = typeof availableColors[color] === 'function' ? availableColors[color] : white @@ -71,7 +71,7 @@ coloredColorizer.message = colored.message coloredColorizer.greyMessage = colored.greyMessage function customColoredColorizerFactory (customColors) { - const customColored = resolveCustomColored(customColors) + const customColored = resolveCustomColoredColorizer(customColors) const customColoredColorizer = function (level, opts) { return colorizeLevel(level, customColored, opts) @@ -88,6 +88,7 @@ function customColoredColorizerFactory (customColors) { * * @param {boolean} [useColors=false] When `true` a function that applies standard * terminal colors is returned. + * @param {array[]} [customColors] Touple where first item of each array is the level index and the second item is the color * * @returns {function} `function (level) {}` has a `.message(str)` method to * apply colorization to a string. The core function accepts either an integer diff --git a/lib/utils.js b/lib/utils.js index 90ac2e00..b8dbf93c 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -217,8 +217,10 @@ function prettifyErrorLog ({ * @param {object} input.log The log object. * @param {function} [input.colorizer] A colorizer function that accepts a level * value and returns a colorized string. Default: a no-op colorizer. - * @param {string} [levelKey='level'] The key to find the level under. + * @param {string} [input.levelKey='level'] The key to find the level under. * @param {function} [input.prettifier] A user-supplied formatter to be called instead of colorizer. + * @param {object} [input.customLevels] The custom levels where key as the level index and value as the level name. + * @param {object} [input.customLevelNames] The custom level names where key is the level name and value is the level index. * * @returns {undefined|string} If `log` does not have a `level` property then * `undefined` will be returned. Otherwise, a string from the specified @@ -242,12 +244,15 @@ function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KE * @param {function} [input.colorizer] A colorizer function that has a * `.message(str)` method attached to it. This function should return a colorized * string which will be the "prettified" message. Default: a no-op colorizer. + * @param {string} [input.levelLabel='levelLabel'] The label used to output the log level + * @param {string} [input.levelKey='level'] The key to find the level under. + * @param {object} [input.customLevels] The custom levels where key as the level index and value as the level name. * * @returns {undefined|string} If the message key is not found, or the message * 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, customLevels, levelKey = LEVEL_KEY }) { +function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL, levelKey = LEVEL_KEY, customLevels }) { if (messageFormat && typeof messageFormat === 'string') { const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) { // return log level as string instead of int diff --git a/test/basic.test.js b/test/basic.test.js index 26a71498..b7467cf8 100644 --- a/test/basic.test.js +++ b/test/basic.test.js @@ -627,10 +627,10 @@ test('basic prettifier tests', (t) => { t.plan(7) const expectedLines = [ ' msg: {', - ' "a": "[Circular]",', ' "b": {', ' "c": "d"', - ' }', + ' },', + ' "a": "[Circular ~]"', ' }' ] const pretty = prettyFactory() diff --git a/test/lib/colors.test.js b/test/lib/colors.test.js index 89e5866b..b4d83b6f 100644 --- a/test/lib/colors.test.js +++ b/test/lib/colors.test.js @@ -76,7 +76,7 @@ const testColoringColorizer = getColorizer => async t => { t.equal(colorized, '\u001B[90mfoo\u001B[39m') } -const testCustomColoringColorizer = getColorizer = async t => { +const testCustomColoringColorizer = getColorizer => async t => { const customLevels = { 0: 'INFO', 1: 'ERR', @@ -120,4 +120,4 @@ test('returns default colorizer - public export', testDefaultColorizer(getColori test('returns colorizing colorizer - private export', testColoringColorizer(getColorizerPrivate)) test('returns colorizing colorizer - public export', testColoringColorizer(getColorizerPublic)) test('returns custom colorizing colorizer - private export', testCustomColoringColorizer(getColorizerPrivate)) -test('returns custom colorizing colorizer - public export', testCustomColoringColorizer(getColorizerPublic)) \ No newline at end of file +test('returns custom colorizing colorizer - public export', testCustomColoringColorizer(getColorizerPublic))