Skip to content

Commit a833c9e

Browse files
committed
console: colorize console error and warn
prints console error in red and warn in yellow PR-URL: #51629 Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Ruy Adorno <ruy@vlt.sh> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent fe4e569 commit a833c9e

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

lib/internal/console/constructor.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
ArrayIsArray,
99
ArrayPrototypeForEach,
1010
ArrayPrototypePush,
11+
ArrayPrototypeSome,
1112
ArrayPrototypeUnshift,
1213
Boolean,
1314
ErrorCaptureStackTrace,
@@ -67,6 +68,7 @@ const {
6768
CHAR_LOWERCASE_N: kTraceInstant,
6869
CHAR_UPPERCASE_C: kTraceCount,
6970
} = require('internal/constants');
71+
const { styleText } = require('util');
7072
const kCounts = Symbol('counts');
7173

7274
const kTraceConsoleCategory = 'node,node.console';
@@ -273,7 +275,7 @@ ObjectDefineProperties(Console.prototype, {
273275
[kWriteToConsole]: {
274276
__proto__: null,
275277
...consolePropAttributes,
276-
value: function(streamSymbol, string) {
278+
value: function(streamSymbol, string, color = '') {
277279
const ignoreErrors = this._ignoreErrors;
278280
const groupIndent = this[kGroupIndent];
279281

@@ -288,6 +290,11 @@ ObjectDefineProperties(Console.prototype, {
288290
}
289291
string = groupIndent + string;
290292
}
293+
294+
if (color) {
295+
string = styleText(color, string);
296+
}
297+
291298
string += '\n';
292299

293300
if (ignoreErrors === false) return stream.write(string);
@@ -378,12 +385,15 @@ const consoleMethods = {
378385
log(...args) {
379386
this[kWriteToConsole](kUseStdout, this[kFormatForStdout](args));
380387
},
381-
382-
383388
warn(...args) {
384-
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
389+
const color = (shouldColorize(args) && 'yellow') || '';
390+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
385391
},
386392

393+
error(...args) {
394+
const color = (shouldColorize(args) && 'red') || '';
395+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), color);
396+
},
387397

388398
dir(object, options) {
389399
this[kWriteToConsole](kUseStdout, inspect(object, {
@@ -681,6 +691,12 @@ const iterKey = '(iteration index)';
681691

682692
const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v);
683693

694+
// TODO: remove string type check once the styleText supports objects
695+
// Return true if all args are type string
696+
const shouldColorize = (args) => {
697+
return lazyUtilColors().hasColors && !ArrayPrototypeSome(args, (arg) => typeof arg !== 'string');
698+
};
699+
684700
function noop() {}
685701

686702
for (const method of ReflectOwnKeys(consoleMethods))
@@ -689,7 +705,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
689705
Console.prototype.debug = Console.prototype.log;
690706
Console.prototype.info = Console.prototype.log;
691707
Console.prototype.dirxml = Console.prototype.log;
692-
Console.prototype.error = Console.prototype.warn;
693708
Console.prototype.groupCollapsed = Console.prototype.group;
694709

695710
function initializeGlobalConsole(globalConsole) {

test/parallel/test-repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,7 @@ const errorTests = [
770770
'Object [console] {',
771771
' log: [Function: log],',
772772
' warn: [Function: warn],',
773+
' error: [Function: error],',
773774
' dir: [Function: dir],',
774775
' time: [Function: time],',
775776
' timeEnd: [Function: timeEnd],',
@@ -785,7 +786,6 @@ const errorTests = [
785786
/ {2}debug: \[Function: (debug|log)],/,
786787
/ {2}info: \[Function: (info|log)],/,
787788
/ {2}dirxml: \[Function: (dirxml|log)],/,
788-
/ {2}error: \[Function: (error|warn)],/,
789789
/ {2}groupCollapsed: \[Function: (groupCollapsed|group)],/,
790790
/ {2}Console: \[Function: Console],?/,
791791
...process.features.inspector ? [
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
3-
(Use `* --trace-warnings ...` to show where the warning was created)
2+
*(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
3+
(Use `* --trace-warnings ...` to show where the warning was created)*
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11

2-
(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
3-
(Use `* --trace-warnings ...` to show where the warning was created)
2+
*(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
3+
(Use `* --trace-warnings ...` to show where the warning was created)*
+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2-
(Use `* --trace-warnings ...` to show where the warning was created)
1+
*(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2+
(Use `* --trace-warnings ...` to show where the warning was created)*

0 commit comments

Comments
 (0)