From 2dba8b2d17eb0b6459ef3b47985a66c4aa35752f Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 14 Jan 2025 14:24:13 +0100 Subject: [PATCH 1/2] fixup Signed-off-by: Matteo Collina --- lib/colors.js | 10 +++++++--- lib/utils/prettify-error-log.test.js | 4 +++- lib/utils/prettify-object.js | 2 +- lib/utils/prettify-object.test.js | 16 ++++++++++++++-- 4 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lib/colors.js b/lib/colors.js index f135c338..321b7b7e 100644 --- a/lib/colors.js +++ b/lib/colors.js @@ -10,13 +10,14 @@ const plain = { 20: nocolor, 10: nocolor, message: nocolor, - greyMessage: nocolor + greyMessage: nocolor, + property: nocolor } const { createColors } = require('colorette') const getLevelLabelData = require('./utils/get-level-label-data') const availableColors = createColors({ useColor: true }) -const { white, bgRed, red, yellow, green, blue, gray, cyan } = availableColors +const { white, bgRed, red, yellow, green, blue, gray, cyan, magenta } = availableColors const colored = { default: white, @@ -27,7 +28,8 @@ const colored = { 20: blue, 10: gray, message: cyan, - greyMessage: gray + greyMessage: gray, + property: magenta } function resolveCustomColoredColorizer (customColors) { @@ -56,6 +58,7 @@ function plainColorizer (useOnlyCustomProps) { } customColoredColorizer.message = plain.message customColoredColorizer.greyMessage = plain.greyMessage + customColoredColorizer.property = plain.property customColoredColorizer.colors = createColors({ useColor: false }) return customColoredColorizer } @@ -66,6 +69,7 @@ function coloredColorizer (useOnlyCustomProps) { return newColoredColorizer(level, colored, opts) } customColoredColorizer.message = colored.message + customColoredColorizer.property = colored.property customColoredColorizer.greyMessage = colored.greyMessage customColoredColorizer.colors = availableColors return customColoredColorizer diff --git a/lib/utils/prettify-error-log.test.js b/lib/utils/prettify-error-log.test.js index 04e10f82..3c9ab093 100644 --- a/lib/utils/prettify-error-log.test.js +++ b/lib/utils/prettify-error-log.test.js @@ -2,6 +2,7 @@ const tap = require('tap') const prettifyErrorLog = require('./prettify-error-log') +const colors = require('../colors') const { ERROR_LIKE_KEYS, MESSAGE_KEY @@ -13,7 +14,8 @@ const context = { customPrettifiers: {}, errorLikeObjectKeys: ERROR_LIKE_KEYS, errorProps: [], - messageKey: MESSAGE_KEY + messageKey: MESSAGE_KEY, + objectColorizer: colors() } tap.test('returns string with default settings', async t => { diff --git a/lib/utils/prettify-object.js b/lib/utils/prettify-object.js index 5de0f5cb..b6ee0fcd 100644 --- a/lib/utils/prettify-object.js +++ b/lib/utils/prettify-object.js @@ -92,7 +92,7 @@ function prettifyObject ({ lines = lines.replace(/\\\\/gi, '\\') const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol }) - result += `${ident}${keyName}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}` + result += `${ident}${objectColorizer.property(keyName)}:${joinedLines.startsWith(eol) ? '' : ' '}${joinedLines}${eol}` }) } diff --git a/lib/utils/prettify-object.test.js b/lib/utils/prettify-object.test.js index 2dde4a9a..9612eaf9 100644 --- a/lib/utils/prettify-object.test.js +++ b/lib/utils/prettify-object.test.js @@ -6,7 +6,6 @@ const prettifyObject = require('./prettify-object') const { ERROR_LIKE_KEYS } = require('../constants') -const getColorizer = require('../colors') const context = { EOL: '\n', @@ -15,7 +14,7 @@ const context = { errorLikeObjectKeys: ERROR_LIKE_KEYS, objectColorizer: colors(), singleLine: false, - colorizer: getColorizer() + colorizer: colors() } tap.test('returns empty string if no properties present', async t => { @@ -152,3 +151,16 @@ tap.test('errors skips prettifying if no lines are present', async t => { }) t.equal(str, '') }) + +tap.test('works with single level properties', async t => { + const colorizer = colors(true) + const str = prettifyObject({ + log: { foo: 'bar' }, + context: { + ...context, + objectColorizer: colorizer, + colorizer + } + }) + t.equal(str, ` ${colorizer.colors.magenta('foo')}: "bar"\n`) +}) From 783f8bd5bf2b734eaad192c336215eaeb3036dad Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Tue, 14 Jan 2025 14:29:05 +0100 Subject: [PATCH 2/2] fixup Signed-off-by: Matteo Collina --- Readme.md | 2 +- bin.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index 2f35e7f2..59ce23c8 100644 --- a/Readme.md +++ b/Readme.md @@ -57,7 +57,7 @@ node app.js | pino-pretty ### CLI Arguments - `--colorize` (`-c`): Adds terminal color escape sequences to the output. -- `--colorizeObjects` (`-C`): Allows suppressing colorization of objects when set to `false`. In combination with `--singleLine`, this ensures that the end of each line is parsable JSON. +- `--no-colorizeObjects`: Suppress colorization of objects. - `--crlf` (`-f`): Appends carriage return and line feed, instead of just a line feed, to the formatted log line. - `--errorProps` (`-e`): When formatting an error object, display this list diff --git a/bin.js b/bin.js index d9d23aea..b0736a63 100644 --- a/bin.js +++ b/bin.js @@ -41,7 +41,6 @@ if (cmd.h || cmd.help) { let opts = minimist(process.argv, { alias: { colorize: 'c', - colorizeObjects: 'C', crlf: 'f', errorProps: 'e', levelFirst: 'l',