From 794e450ea7746873ad0ae23d40220cabfeb19c60 Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Thu, 1 Feb 2024 19:26:01 +1100 Subject: [PATCH] console: colorize console error and warn prints console error in red and warn in yellow PR-URL: https://github.com/nodejs/node/pull/51629 Reviewed-By: Moshe Atlow Reviewed-By: Ruy Adorno Reviewed-By: James M Snell --- lib/internal/console/constructor.js | 25 +++++++++++++++---- test/parallel/test-repl.js | 2 +- .../test-tty-color-support-warning-2.out | 4 +-- .../test-tty-color-support-warning.out | 4 +-- test/pseudo-tty/test-tty-color-support.out | 4 +-- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index 4d74f4d4df3668..e3c7a2cb5b36f8 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -8,6 +8,7 @@ const { ArrayIsArray, ArrayPrototypeForEach, ArrayPrototypePush, + ArrayPrototypeSome, ArrayPrototypeUnshift, Boolean, ErrorCaptureStackTrace, @@ -67,6 +68,7 @@ const { CHAR_LOWERCASE_N: kTraceInstant, CHAR_UPPERCASE_C: kTraceCount, } = require('internal/constants'); +const { styleText } = require('util'); const kCounts = Symbol('counts'); const kTraceConsoleCategory = 'node,node.console'; @@ -273,7 +275,7 @@ ObjectDefineProperties(Console.prototype, { [kWriteToConsole]: { __proto__: null, ...consolePropAttributes, - value: function(streamSymbol, string) { + value: function(streamSymbol, string, color = '') { const ignoreErrors = this._ignoreErrors; const groupIndent = this[kGroupIndent]; @@ -288,6 +290,11 @@ ObjectDefineProperties(Console.prototype, { } string = groupIndent + string; } + + if (color) { + string = styleText(color, string); + } + string += '\n'; if (ignoreErrors === false) return stream.write(string); @@ -378,12 +385,15 @@ const consoleMethods = { log(...args) { this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args)); }, - - warn(...args) { - this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args)); + const color = (shouldColorize(args) && 'yellow') || ''; + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color); }, + error(...args) { + const color = (shouldColorize(args) && 'red') || ''; + this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color); + }, dir(object, options) { this[kWriteToConsole](kUseStdout, inspect(object, { @@ -676,6 +686,12 @@ const iterKey = '(iteration index)'; const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v); +// TODO: remove string type check once the styleText supports objects +// Return true if all args are type string +const shouldColorize = (args) => { + return lazyUtilColors().hasColors && !ArrayPrototypeSome(args, (arg) => typeof arg !== 'string'); +}; + function noop() {} for (const method of ReflectOwnKeys(consoleMethods)) @@ -684,7 +700,6 @@ for (const method of ReflectOwnKeys(consoleMethods)) Console.prototype.debug = Console.prototype.log; Console.prototype.info = Console.prototype.log; Console.prototype.dirxml = Console.prototype.log; -Console.prototype.error = Console.prototype.warn; Console.prototype.groupCollapsed = Console.prototype.group; function initializeGlobalConsole(globalConsole) { diff --git a/test/parallel/test-repl.js b/test/parallel/test-repl.js index 74299d4b4cbad5..b798ad71a0e960 100644 --- a/test/parallel/test-repl.js +++ b/test/parallel/test-repl.js @@ -764,6 +764,7 @@ const errorTests = [ 'Object [console] {', ' log: [Function: log],', ' warn: [Function: warn],', + ' error: [Function: error],', ' dir: [Function: dir],', ' time: [Function: time],', ' timeEnd: [Function: timeEnd],', @@ -779,7 +780,6 @@ const errorTests = [ / {2}debug: \[Function: (debug|log)],/, / {2}info: \[Function: (info|log)],/, / {2}dirxml: \[Function: (dirxml|log)],/, - / {2}error: \[Function: (error|warn)],/, / {2}groupCollapsed: \[Function: (groupCollapsed|group)],/, / {2}Console: \[Function: Console],?/, ...process.features.inspector ? [ diff --git a/test/pseudo-tty/test-tty-color-support-warning-2.out b/test/pseudo-tty/test-tty-color-support-warning-2.out index 37b470a5f108f9..61a1baa0b54fb6 100644 --- a/test/pseudo-tty/test-tty-color-support-warning-2.out +++ b/test/pseudo-tty/test-tty-color-support-warning-2.out @@ -1,3 +1,3 @@ -(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. -(Use `* --trace-warnings ...` to show where the warning was created) +*(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `* --trace-warnings ...` to show where the warning was created)* diff --git a/test/pseudo-tty/test-tty-color-support-warning.out b/test/pseudo-tty/test-tty-color-support-warning.out index b25d2e42cf7244..35ed65c96d29ad 100644 --- a/test/pseudo-tty/test-tty-color-support-warning.out +++ b/test/pseudo-tty/test-tty-color-support-warning.out @@ -1,3 +1,3 @@ -(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. -(Use `* --trace-warnings ...` to show where the warning was created) +*(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `* --trace-warnings ...` to show where the warning was created)* diff --git a/test/pseudo-tty/test-tty-color-support.out b/test/pseudo-tty/test-tty-color-support.out index df5831c555be19..55b3986f73b2f0 100644 --- a/test/pseudo-tty/test-tty-color-support.out +++ b/test/pseudo-tty/test-tty-color-support.out @@ -1,2 +1,2 @@ -(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. -(Use `* --trace-warnings ...` to show where the warning was created) +*(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set. +(Use `* --trace-warnings ...` to show where the warning was created)*