Node.js new Console() Method
Last Updated :
03 Nov, 2021
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
const fs = require( 'fs' );
const { Console } = require( 'console' );
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;
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
const fs = require( 'fs' );
const console = require( 'console' );
const { Console } = console;
try {
const output = fs.createWriteStream( './outputlog.txt' );
const error = fs.createWriteStream( './errlog.txt' );
const objLogger = new Console(
{ stdout: output, stderr: error,
ignoreErrors: true , colorMode: true }
);
const outt = fs.createWriteStream( './output.log' );
const err = fs.createWriteStream( './error.log' );
const logObject = new console.Console(outt, err);
var family = {};
family.Head = 'Miss Sanno' ;
family.headDesignation = 'Teacher' ;
family.Member1 = 'Miss Sanchi' ;
family.member1Designation = 'Kid' ;
family.Member2 = 'Master Amit' ;
family.member2Designation = 'Student' ;
const count = 25+75*5-5/2;
objLogger.log( 'Family: %s' , family);
console.log( 'Family Stream Created: ' , family);
logObject.log( 'Count: %s' , count);
console.log( 'Count Stream Created: ' , count);
}
catch {
console.error( new Error(
'Oops, some error happened in family...' ));
}
|
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
Share your thoughts in the comments
Please Login to comment...