Node.js Customize Console Class
Last Updated :
24 Jun, 2021
The Console class can be used to create a logger with configurable output streams and the basic usage is described in Node.js new Console() Method. But as you might observe, some options only allow boolean value as input.
For example, enabling colorMode is useful when you have various output data. Node.js provides quite a handy setting to display the console logs. However, in some situations, you may still want to modify it to fit your need without installing any additional packages.
In this article, we will first go through how to make use of inspectOptions for some other advanced settings of the Console class and then explain how to modify the colorMode details.
Example 1: Using the default Console class. The way it showcases the data is the same as the global console, i.e. console.log.
index.js
const { Console } = require( 'console' );
const logger = new Console({
stdout: process.stdout,
stderr: process.stderr,
});
logger.log( 'log: object' , {attr:
'string content a b c d e f g h i j k' });
logger.log( 'log: array' , [ 'array_value1' , 'array_value2' ,
'array_value3' , 'array_value4' , 'array_value5' ]);
logger.log( 'log: set' , new Set([3, 1, 2, 5, 4]));
|
Run index.js file using the following command:
node index.js
Output:
log: object { attr: 'string content a b c d e f g h i j k' }
log: array [
'array_value1',
'array_value2',
'array_value3',
'array_value4',
'array_value5'
]
log: set Set(5) { 3, 1, 2, 5, 4 }
Example 2: If we don’t have enough space to show everything and want it to be shorter and well-organized. We will specify the following attributes in inspectOptions object and attach them to the Console options.
- maxArrayLength: It is used to specify the maximum number of Array elements to include when formatting. Default: 100.
- maxStringLength: It is used to specify the maximum number of characters to include when formatting. Default: 10000.
- sorted: If this is set to true the default sort is used and if this is set to a function, it is used as a compare function.
index.js
const { Console } = require( 'console' );
const logger = new Console({
stdout: process.stdout,
stderr: process.stderr,
inspectOptions: {
maxArrayLength: 3,
maxStringLength: 10,
sorted: true ,
}
});
logger.log( 'log: object' , {attr:
'string content a b c d e f g h i j k' });
logger.log( 'log: array' , [ 'array_value1' , 'array_value2' ,
'array_value3' , 'array_value4' , 'array_value5' ]);
logger.log( 'log: set' , new Set([3, 1, 2, 5, 4]));
|
Run index.js file using the following command:
node index.js
Output:
log: object { attr: ‘string content a b c’… 16 more characters }
log: array [ ‘array_value1’, ‘array_value2’, ‘array_value3’, … 2 more items ]
log: set Set(5) { 1, 2, 3, 4, 5 }
Explanation: If you took a look at the inspectOptions accepted parameters, there is one called colors. This parameter has a higher priority than colorMode in Console class. If inspectOptions.colors is set, colorMode is ignored. Again, it allows boolean value only. That is, it accepts true or false. This may be disappointing but here is a workaround if there is only 1 logger to handle at a time.
Example 3: We can modify the default setting in util.inspect.styles as the sample code below. (Here is the list of all the values acceptable for each data type). Do note that this is an approach to modify the global color scheme. Once the change is applied, all the other components enabling the color mode and following this color scheme are affected.
index.js
const { Console } = require( 'console' );
const util = require( 'util' );
util.inspect.styles.number = 'red' ;
const logger = new Console({
stdout: process.stdout,
stderr: process.stderr,
inspectOptions: {
colors: true
}
});
logger.log( 'log: set' , new Set([3, 1, 2, 5, 4]));
|
Run index.js file using the following command:
node index.js
Output:
log: set Set { 3, 1, 2, 5, 4 }
Reference: https://nodejs.org/api/util.html#util_customizing_util_inspect_colors
Similar Reads
Node.js console.clear() Method
The console.clear() method is used to clear the stdout, when stdout is a TTY (Teletype) i.e. terminal it will attempt to clear the TTY. When stdout is not a TTY, this method does nothing. The console.clear() will work differently across different operating systems and terminal types. For Linux opera
1 min read
Node.js console.assert() Method
The console.assert() method is an inbuilt application programming interface of the console module which is used to assert value passed to it as a parameter, i.e. it checks whether the value is true or not, and prints an error message, if provided and failed to assert the value. Syntax: console.asser
2 min read
Node.js Console
The console module is essential for debugging and logging in Node.js applications. It enables developers to print messages to the terminal, making it easier to monitor application behavior, track issues, and display runtime information. Console in Node.js The console module in Node.js is a built-in
4 min read
Node.js console.log() Function
The console.log() function from console class of Node.js is used to display the messages on the console. It prints to stdout with newline. Syntax: console.log( [data][, ...] ) Parameter: This function contains multiple parameters which are to be printed. Return type: The function returns the passed
1 min read
Node.js console.count() Method
The console.count() method is an inbuilt application programming interface of the console module which is used to count label passed to it as a parameter, by maintaining an internal counter for that specific label. Syntax: console.count(label) Parameters: This method has one parameter as mentioned a
2 min read
Node.js console.warn() Function
The console.warn() function from console class of Node.js is used to display the warning messages on the console. It prints to stderr with newline. Note: This function is an alias of console.error() function. Syntax: console.warn( [data][, ...args] ) Parameter: This function can contains multiple pa
1 min read
Node.js console.error() Function
The console.error() function from the console class of Node.js is used to display an error message on the console. It prints to stderr with a newline. Syntax: console.error([data][, ...args]) Parameter: This function can contain multiple parameters. The first parameter is used for the primary messag
1 min read
Node.js console.countReset() Method
The console.countReset() method is an inbuilt application programming interface of the console module which is used to reset the count for the specific label passed to it as a parameter. Syntax: console.countReset( label ); Parameters: This method has one parameter as mentioned above and described b
2 min read
Node.js console.dir() Method
The console.dir() method is used to get the list of object properties of a specified object. These object properties also have child objects, from which you can inspect for further information. Syntax: console.dir( object ) Parameters: This method accepts single parameter which holds the object elem
1 min read
Node.js new Console() Method
The console module provides a simple debugging console that is provided by web browsers which export two specific components: A Console class that can be used to write to any Node.js stream. Example: console.log(), console.error(), etc.A global console that can be used without importing console. Exa
4 min read