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' ) ;
7072const kCounts = Symbol ( 'counts' ) ;
7173
7274const kTraceConsoleCategory = '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 const ignoreErrors = this . _ignoreErrors ;
278280 const groupIndent = 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 ) return stream . 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+ const color = ( shouldColorize ( args ) && 'yellow' ) || '' ;
390+ this [ kWriteToConsole ] ( kUseStderr , this [ kFormatForStderr ] ( args ) , color ) ;
385391 } ,
386392
393+ error ( ...args ) {
394+ const color = ( 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
677687const isArray = ( 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+ const shouldColorize = ( args ) => {
692+ return lazyUtilColors ( ) . hasColors && ! ArrayPrototypeSome ( args , ( arg ) => typeof arg !== 'string' ) ;
693+ } ;
694+
679695function noop ( ) { }
680696
681697for ( const method of ReflectOwnKeys ( consoleMethods ) )
@@ -684,7 +700,6 @@ for (const method of ReflectOwnKeys(consoleMethods))
684700Console . prototype . debug = Console . prototype . log ;
685701Console . prototype . info = Console . prototype . log ;
686702Console . prototype . dirxml = Console . prototype . log ;
687- Console . prototype . error = Console . prototype . warn ;
688703Console . prototype . groupCollapsed = Console . prototype . group ;
689704
690705function initializeGlobalConsole ( globalConsole ) {
0 commit comments