”;
A console is a text-based user interface that allows you to interact with the computer”s operating system or run code. With the console, you can write and execute commands or programs. It is also used for debugging and troubleshooting. The built-in library of Node.js includes the Console module. It provides functionality to print messages to IO streams, a functionality similar to the JavaScript console mechanism provided by web browsers.
The Console module’s functionality has two major parts −
Console class: The console class methods are console.log(), console.error() and console.warn() to display Node.js stream.
global console object: A predefined object that echoes logging, errors and warning messages to the standard output stream. It is used without calling require(‘console’).
The default usage of global console object is simple. The following code shows a typical usage of global console object to display logging, error and warning messages.
Example
// Welcome message console.log(''Welcome to Tutorialspoint!''); // Hello world message console.log(''hello world''); // printing to error stream console.error(new Error(''Oops... something went wrong'')); // print warning const name = ''NodeJS Tutorial''; console.warn(`Warning ${name}! Warning!`);
Output
Welcome to Tutorialspoint! hello world Error: Oops... something went wrong at Object.<anonymous> (D:nodejsemailappmain.js:11:15) at Module._compile (node:internal/modules/cjs/loader:1241:14) at Module._extensions..js (node:internal/modules/cjs/loader:1295:10) at Module.load (node:internal/modules/cjs/loader:1091:32) at Module._load (node:internal/modules/cjs/loader:938:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47 Warning NodeJS Tutorial! Warning!
You can also customize the console object by designating the streams for reflecting the output and error logs. Declare an object of Console class and pass the stream objects as arguments.
The program below redirects the logs and errors to the two disk files.
const fs = require(''fs''); const out = fs.createWriteStream(''./stdout.log''); const err = fs.createWriteStream(''./stderr.log''); const logger = new console.Console(out, err); // Welcome message logger.log(''Welcome to Tutorialspoint!''); // Hello world message logger.log(''hello world''); // printing to error stream logger.error(new Error(''Oops... something went wrong'')); // print warning const name = ''NodeJS Tutorial''; logger.warn(`Warning ${name}! Warning!`);
This will create two files stdout.log and stderr.log in the current directory, with the result of log(), error(), and warn() methods of console objects saved in them.
Console Methods
Following is a list of methods available with the console object.
Sr.No. | Method & Description |
---|---|
1 |
console.log([data][, …]) Prints to stdout with newline. This function can take multiple arguments in a printf()-like way. |
2 |
console.info([data][, …]) Prints to stdout with newline. This function can take multiple arguments in a printf()-like way. |
3 |
console.error([data][, …]) Prints to stderr with newline. This function can take multiple arguments in a printf()-like way. |
4 |
console.warn([data][, …]) Prints to stderr with newline. This function can take multiple arguments in a printf()-like way |
5 |
console.dir(obj[, options]) Uses util.inspect on obj and prints resulting string to stdout. |
6 |
console.time(label) Mark a time. |
7 |
console.timeEnd(label) Finish timer, record output. |
8 |
console.trace(message[, …]) Print to stderr ”Trace :”, followed by the formatted message and stack trace to the current position. |
9 |
console.assert(value[, message][, …]) Similar to assert.ok(), but the error message is formatted as util.format(message…). |
”;