Open In App

Node.js new Console() Method

Last Updated : 03 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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. Example: process.stdout, process.stderr, etc.

The new Console() (Added in v8.0.0) method is an inbuilt application programming interface of the ‘console’ module which creates a new Console with single or multiple writable stream instances in which stdout is a writable stream and stderr is used for warning or error output. If stderr is not provided, stdout is used for stderr. It is a console whose output is sent to process.stdout and process.stderr.

Note: The global console methods are neither consistently synchronous nor consistently asynchronous.

Syntax:

new Console(options);

Arguments:

const options = { 
   stdout: writableStream, 
   stderr: writableStream, 
   ignoreErrors: true, 
   colorMode:true 
};

In order to use this method, we need to create a console using (new Console()) method, and we need to import the ‘console’ and ‘fs‘ module.

const console = require('console');
const fs = require('fs');  

Parameters: This function accepts an object/list of parameters as mentioned above and described below:

options <Object>: It may have the following elements in the options object.

  • stdout <stream.Writable>: It accepts the write stream which is imported from fs module.
  • stderr <stream.Writable>: It also accepts the write stream which is imported from fs module.
  • ignoreErrors <boolean>: The default value passed is true. It ignores the errors while writing to the underlying streams.
  • colorMode <boolean> | <string>: The default value passed is ‘auto‘. It is used to set color which supports this Console instance only. It could be set true, false, or ‘auto’ according to set color mode.
  • inspectOptions <Object>: It specifies the options that are passed along to util.inspect() method.
  • groupIndentation <number>: The default value set is 2. It is used to set group indentation.

Return Value: Its output is sent to process.stdout and process.stderr files which are created by fs module through <stream.Writable>.

The Below examples illustrate the use of new Console(options) method in Node.js.

Example 1:  Filename: index.js

javascript




// Node.js program to demonstrate the
// new Console() method
 
// Using require to access fs module
const fs = require('fs');
 
// Using require to access console module
const { Console } = require('console');
 
// Creating write Stream
const output = fs.createWriteStream('./out.log');
const errorOutput = fs.createWriteStream('./err.log');
 
//
const options = { stdout: output, stderr: errorOutput,
ignoreErrors: true, colorMode: false };
const logger = new Console(options);
 
const count = 5;
 
// Using like console
logger.log('count: %d', count);
console.log("Successfully created and logged via console...", )


Run index.js file using the following command:

node index.js

Output:

Note: The above node.js example will create log files (out & err) in the same folder where index.js file exists.

>> Successfully created and logged via console…

Example 2:  Filename: index.js

javascript




// Node.js program to demonstrate the
// new Console() method
 
// Using require to access fs module
const fs = require('fs');
 
// Using require to access console module
const console = require('console');
const { Console } = console;
 
try {
 // Creating write Stream
 const output = fs.createWriteStream('./outputlog.txt');
 const error = fs.createWriteStream('./errlog.txt');
 
 // Creating new Console
 const objLogger   = new Console(
    { stdout: output, stderr: error,
     ignoreErrors: true, colorMode: true }
);
 
 // Custom write Stream
 const outt = fs.createWriteStream('./output.log');
 const err = fs.createWriteStream('./error.log');
   
 // Another way to create console
 // (default values are passed to options)
 const logObject = new console.Console(outt, err);
 
 // Creating family object
 var family = {};
 family.Head = 'Miss Sanno';
 family.headDesignation = 'Teacher';
 family.Member1 = 'Miss Sanchi';
 family.member1Designation = 'Kid';
 family.Member2 = 'Master Amit';
 family.member2Designation = 'Student';
 
 // Creating constant value count
 const count = 25+75*5-5/2;
 // Writing via console
 objLogger.log('Family: %s', family);
 // Printing Family Object to console
 console.log('Family Stream Created: ', family);
 // Writing via console
 logObject.log('Count: %s', count);
 // Printing count to console
 console.log('Count Stream Created: ', count);
 // console.log(logObject.family.error)
 }
catch {
  console.error(new Error(
    'Oops, some error happened in family...'));
  // Prints: [Error: Oops, some error
  // happened in family...], to stderr
}


Run index.js file using the following command:

node index.js

Output:

Note: The above node.js example will create log files (output & error) in the same folder where index.js file exists. 
Family Stream Created: { 
Head: ‘Miss Sanno’, 
headDesignation: ‘Teacher’, 
Member1: ‘Miss Sanchi’, 
member1Designation: ‘Kid’, 
Member2: ‘Master Amit’, 
member2Designation: ‘Student’ 

Count Stream Created:  397.5 
 

Reference: https://nodejs.org/api/console.html#console_new_console_options



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads