8
8
ArrayIsArray,
9
9
ArrayPrototypeForEach,
10
10
ArrayPrototypePush,
11
+ ArrayPrototypeSome,
11
12
ArrayPrototypeUnshift,
12
13
Boolean,
13
14
ErrorCaptureStackTrace,
@@ -67,6 +68,7 @@ const {
67
68
CHAR_LOWERCASE_N : kTraceInstant ,
68
69
CHAR_UPPERCASE_C : kTraceCount ,
69
70
} = require ( 'internal/constants' ) ;
71
+ const { styleText } = require ( 'util' ) ;
70
72
const kCounts = Symbol ( 'counts' ) ;
71
73
72
74
const kTraceConsoleCategory = 'node,node.console' ;
@@ -273,7 +275,7 @@ ObjectDefineProperties(Console.prototype, {
273
275
[ kWriteToConsole ] : {
274
276
__proto__ : null ,
275
277
...consolePropAttributes ,
276
- value : function ( streamSymbol , string ) {
278
+ value : function ( streamSymbol , string , color = '' ) {
277
279
const ignoreErrors = this . _ignoreErrors ;
278
280
const groupIndent = this [ kGroupIndent ] ;
279
281
@@ -288,6 +290,11 @@ ObjectDefineProperties(Console.prototype, {
288
290
}
289
291
string = groupIndent + string ;
290
292
}
293
+
294
+ if ( color ) {
295
+ string = styleText ( color , string ) ;
296
+ }
297
+
291
298
string += '\n' ;
292
299
293
300
if ( ignoreErrors === false ) return stream . write ( string ) ;
@@ -378,12 +385,15 @@ const consoleMethods = {
378
385
log ( ...args ) {
379
386
this [ kWriteToConsole ] ( kUseStdout , this [ kFormatForStdout ] ( args ) ) ;
380
387
} ,
381
-
382
-
383
388
warn ( ...args ) {
384
- this [ kWriteToConsole ] ( kUseStderr , this [ kFormatForStderr ] ( args ) ) ;
389
+ const color = ( shouldColorize ( args ) && 'yellow' ) || '' ;
390
+ this [ kWriteToConsole ] ( kUseStderr , this [ kFormatForStderr ] ( args ) , color ) ;
385
391
} ,
386
392
393
+ error ( ...args ) {
394
+ const color = ( shouldColorize ( args ) && 'red' ) || '' ;
395
+ this [ kWriteToConsole ] ( kUseStderr , this [ kFormatForStderr ] ( args ) , color ) ;
396
+ } ,
387
397
388
398
dir ( object , options ) {
389
399
this [ kWriteToConsole ] ( kUseStdout , inspect ( object , {
@@ -676,6 +686,12 @@ const iterKey = '(iteration index)';
676
686
677
687
const isArray = ( v ) => ArrayIsArray ( v ) || isTypedArray ( v ) || isBuffer ( v ) ;
678
688
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
+
679
695
function noop ( ) { }
680
696
681
697
for ( const method of ReflectOwnKeys ( consoleMethods ) )
@@ -684,7 +700,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
684
700
Console . prototype . debug = Console . prototype . log ;
685
701
Console . prototype . info = Console . prototype . log ;
686
702
Console . prototype . dirxml = Console . prototype . log ;
687
- Console . prototype . error = Console . prototype . warn ;
688
703
Console . prototype . groupCollapsed = Console . prototype . group ;
689
704
690
705
function initializeGlobalConsole ( globalConsole ) {
0 commit comments