Skip to content

Commit 0a37526

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

File tree

6 files changed

+39
-28
lines changed

6 files changed

+39
-28
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}${lazyUtilColors().clear}`;
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) {

lib/internal/util/colors.js

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
'use strict';
22

3+
const {
4+
ObjectKeys
5+
} = primordials;
6+
37
let internalTTy;
48
function lazyInternalTTY() {
59
internalTTy ??= require('internal/tty');
610
return internalTTy;
711
}
812

13+
const colorsMap = {
14+
blue: '\u001b[34m',
15+
green: '\u001b[32m',
16+
white: '\u001b[39m',
17+
yellow: '\u001b[33m',
18+
red: '\u001b[31m',
19+
gray: '\u001b[90m',
20+
clear: '\u001b[0m'
21+
}
22+
923
module.exports = {
10-
blue: '',
11-
green: '',
12-
white: '',
13-
red: '',
14-
gray: '',
15-
clear: '',
1624
hasColors: false,
1725
shouldColorize(stream) {
1826
if (process.env.FORCE_COLOR !== undefined) {
@@ -23,18 +31,14 @@ module.exports = {
2331
stream.getColorDepth() > 2 : true);
2432
},
2533
refresh() {
26-
if (process.stderr.isTTY) {
27-
const hasColors = module.exports.shouldColorize(process.stderr);
28-
module.exports.blue = hasColors ? '\u001b[34m' : '';
29-
module.exports.green = hasColors ? '\u001b[32m' : '';
30-
module.exports.white = hasColors ? '\u001b[39m' : '';
31-
module.exports.yellow = hasColors ? '\u001b[33m' : '';
32-
module.exports.red = hasColors ? '\u001b[31m' : '';
33-
module.exports.gray = hasColors ? '\u001b[90m' : '';
34-
module.exports.clear = hasColors ? '\u001bc' : '';
35-
module.exports.hasColors = hasColors;
36-
}
37-
},
34+
const isTTY = process.stderr.isTTY;
35+
const hasColors = isTTY && module.exports.shouldColorize(process.stderr);
36+
ObjectKeys(colorsMap).forEach(key => {
37+
module.exports[key] = hasColors ? colorsMap[key] : '';
38+
});
39+
40+
module.exports.hasColors = hasColors;
41+
}
3842
};
3943

4044
module.exports.refresh();

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 ? [
Lines changed: 2 additions & 2 deletions
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+
[31m(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)[0m
Lines changed: 2 additions & 2 deletions
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+
[31m(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)[0m
Lines changed: 2 additions & 2 deletions
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+
[31m(node:*) Warning: The 'NO_COLOR' env is ignored due to the 'FORCE_COLOR' env being set.
2+
(Use `node --trace-warnings ...` to show where the warning was created)[0m

0 commit comments

Comments
 (0)