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 , {
@@ -681,6 +691,12 @@ const iterKey = '(iteration index)';
681
691
682
692
const isArray = ( v ) => ArrayIsArray ( v ) || isTypedArray ( v ) || isBuffer ( v ) ;
683
693
694
+ // TODO: remove string type check once the styleText supports objects
695
+ // Return true if all args are type string
696
+ const shouldColorize = ( args ) => {
697
+ return lazyUtilColors ( ) . hasColors && ! ArrayPrototypeSome ( args , ( arg ) => typeof arg !== 'string' ) ;
698
+ } ;
699
+
684
700
function noop ( ) { }
685
701
686
702
for ( const method of ReflectOwnKeys ( consoleMethods ) )
@@ -689,7 +705,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
689
705
Console . prototype . debug = Console . prototype . log ;
690
706
Console . prototype . info = Console . prototype . log ;
691
707
Console . prototype . dirxml = Console . prototype . log ;
692
- Console . prototype . error = Console . prototype . warn ;
693
708
Console . prototype . groupCollapsed = Console . prototype . group ;
694
709
695
710
function initializeGlobalConsole ( globalConsole ) {
0 commit comments