Skip to content

Commit 794e450

Browse files
MrJithilmarco-ippolito
authored andcommitted
console: colorize console error and warn
prints console error in red and warn in yellow PR-URL: #51629 Reviewed-By: Moshe Atlow <[email protected]> Reviewed-By: Ruy Adorno <[email protected]> Reviewed-By: James M Snell <[email protected]>
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‎

Lines changed: 20 additions & 5 deletions
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
constkCounts=Symbol('counts');
7173

7274
constkTraceConsoleCategory='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
constignoreErrors=this._ignoreErrors;
278280
constgroupIndent=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)returnstream.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+
constcolor=(shouldColorize(args)&&'yellow')||'';
390+
this[kWriteToConsole](kUseStderr,this[kFormatForStderr](args),color);
385391
},
386392

393+
error(...args){
394+
constcolor=(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
constisArray=(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+
constshouldColorize=(args)=>{
692+
returnlazyUtilColors().hasColors&&!ArrayPrototypeSome(args,(arg)=>typeofarg!=='string');
693+
};
694+
679695
functionnoop(){}
680696

681697
for(constmethodofReflectOwnKeys(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
functioninitializeGlobalConsole(globalConsole){

‎test/parallel/test-repl.js‎

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