Skip to content

Commit 794e450

Browse files
MrJithilmarco-ippolito
authored andcommittedJun 17, 2024
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 403a4a7 commit 794e450

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, {
@@ -676,6 +686,12 @@ const iterKey = '(iteration index)';
676686

677687
const isArray = (v) => ArrayIsArray(v) || isTypedArray(v) || isBuffer(v);
678688

689+
// TODO: remove string type check once the styleText supports objects
690+
// Return true if all args are type string
691+
const shouldColorize = (args) => {
692+
return lazyUtilColors().hasColors && !ArrayPrototypeSome(args, (arg) => typeof arg !== 'string');
693+
};
694+
679695
function noop() {}
680696

681697
for (const method of ReflectOwnKeys(consoleMethods))
@@ -684,7 +700,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
684700
Console.prototype.debug = Console.prototype.log;
685701
Console.prototype.info = Console.prototype.log;
686702
Console.prototype.dirxml = Console.prototype.log;
687-
Console.prototype.error = Console.prototype.warn;
688703
Console.prototype.groupCollapsed = Console.prototype.group;
689704

690705
function initializeGlobalConsole(globalConsole) {

‎test/parallel/test-repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,7 @@ const errorTests = [
764764
'Object [console] {',
765765
' log: [Function: log],',
766766
' warn: [Function: warn],',
767+
' error: [Function: error],',
767768
' dir: [Function: dir],',
768769
' time: [Function: time],',
769770
' timeEnd: [Function: timeEnd],',
@@ -779,7 +780,6 @@ const errorTests = [
779780
/ {2}debug: \[Function: (debug|log)],/,
780781
/ {2}info: \[Function: (info|log)],/,
781782
/ {2}dirxml: \[Function: (dirxml|log)],/,
782-
/ {2}error: \[Function: (error|warn)],/,
783783
/ {2}groupCollapsed: \[Function: (groupCollapsed|group)],/,
784784
/ {2}Console: \[Function: Console],?/,
785785
...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)
Please sign in to comment.