Skip to content

Commit

Permalink
console: colorize console error and warn
Browse files Browse the repository at this point in the history
prints console error in red and warn in yellow
  • Loading branch information
MrJithil committed Feb 1, 2024
1 parent a501315 commit b69e24d
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 7 deletions.
13 changes: 10 additions & 3 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,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];

Expand All @@ -288,6 +288,11 @@ ObjectDefineProperties(Console.prototype, {
}
string = groupIndent + string;
}

if (color && lazyUtilColors().hasColors) {
string = `${color}${string}`;
}

string += '\n';

if (ignoreErrors === false) return stream.write(string);
Expand Down Expand Up @@ -381,9 +386,12 @@ const consoleMethods = {


warn(...args) {
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), lazyUtilColors().yellow);
},

error(...args) {
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), lazyUtilColors().red);
},

dir(object, options) {
this[kWriteToConsole](kUseStdout, inspect(object, {
Expand Down Expand Up @@ -689,7 +697,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) {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,7 @@ const errorTests = [
'Object [console] {',
' log: [Function: log],',
' warn: [Function: warn],',
' error: [Function: error],',
' dir: [Function: dir],',
' time: [Function: time],',
' timeEnd: [Function: timeEnd],',
Expand All @@ -785,7 +786,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 ? [
Expand Down
2 changes: 1 addition & 1 deletion test/pseudo-tty/test-tty-color-support-warning-2.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' env is ignored due to the 'FORCE_COLOR' env being set.
[31m(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)
2 changes: 1 addition & 1 deletion test/pseudo-tty/test-tty-color-support-warning.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

(node:*) Warning: The 'NODE_DISABLE_COLORS' and 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
[31m(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)
2 changes: 1 addition & 1 deletion test/pseudo-tty/test-tty-color-support.out
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
[31m(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)

0 comments on commit b69e24d

Please sign in to comment.