Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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: {
  
        // Maximum number of Array elements 
        // to include when formatting.
        maxArrayLength: 3, 
  
        // Maximum number of characters to 
        // include when formatting.
        maxStringLength: 10, 
  
        // If properties of an object are sorted
        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');
  
// This can be declared after the 
// logger is created, too.
util.inspect.styles.number = 'red';
  
const logger = new Console({
    stdout: process.stdout,
    stderr: process.stderr,
    inspectOptions: {
  
        // Remember to enable the color mode.
        // Default is false.
        colors: true
    }
});
  
// Now the number in the set are all shown in red.
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




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!