Skip to content

Commit 827fe0c

Browse files
committed
console: colorize console error and warn
prints console error in red and warn in yellow
1 parent a501315 commit 827fe0c

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

lib/internal/console/constructor.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ ObjectDefineProperties(Console.prototype, {
273273
[kWriteToConsole]: {
274274
__proto__: null,
275275
...consolePropAttributes,
276-
value: function(streamSymbol, string) {
276+
value: function(streamSymbol, string, color) {
277277
const ignoreErrors = this._ignoreErrors;
278278
const groupIndent = this[kGroupIndent];
279279

@@ -288,6 +288,11 @@ ObjectDefineProperties(Console.prototype, {
288288
}
289289
string = groupIndent + string;
290290
}
291+
292+
if (color && lazyUtilColors().hasColors) {
293+
string = `${color}${string}`;
294+
}
295+
291296
string += '\n';
292297

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

382387

383388
warn(...args) {
384-
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args));
389+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), lazyUtilColors().yellow);
385390
},
386391

392+
error(...args) {
393+
this[kWriteToConsole](kUseStderr, this[kFormatForStderr](args), lazyUtilColors().red);
394+
},
387395

388396
dir(object, options) {
389397
this[kWriteToConsole](kUseStdout, inspect(object, {
@@ -689,7 +697,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
689697
Console.prototype.debug = Console.prototype.log;
690698
Console.prototype.info = Console.prototype.log;
691699
Console.prototype.dirxml = Console.prototype.log;
692-
Console.prototype.error = Console.prototype.warn;
693700
Console.prototype.groupCollapsed = Console.prototype.group;
694701

695702
function initializeGlobalConsole(globalConsole) {

test/parallel/test-repl.js

Lines changed: 1 addition & 1 deletion
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 ? [

0 commit comments

Comments
 (0)