NodeJS - SPLessons

Node.js Console Logging

Home > Lesson > Chapter 12
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Node.js Console Logging

Node.js Console Logging

shape Introduction

The chapter demonstrates about Node.js Console Logging, the Node.js console API are utilized to write the data to console with the help of inbuilt objects. Actually the Node.js console functions are used for debugging. Following are the concepts covered.
  • Node.js Console
  • Console Logging Functions

Node.js Console

shape Description

The function of Node.js console blocking and synchronous while writing to a file or console.  The console functions are non-blocking and asynchronous whenever they are unequivocally queued as a long queue of console functions can block the execution of program for certain time period. The Node.js console functions are commonly used to debug the node programs. The built-in console objects of node contain several methods in order to print to the stderr and stdout.

Console Logging Functions

shape Description

The Node.js REPL (Read-Eval-Print-Loop) is a simple module which can be accessible for both as a standalone program or can be included in different programing applications. JavaScript code can be executed using this prompt. REPL command prompt can be accessed if a Node.js binary is initialized without passing any argument. Following are few methods of built-in console object for printing the stderr and stdout. console.info([data][, ...args]) - Multiple arguments can be given and the function is similar to the console.log. The image below gives few examples of the function console.info(). console.trace(message[, …args]) - Using the function console.trace() stack trace is printed to stderr for the current node.js position, the image below gives the example for console.trace() function. console.time(label) - The function console.time() is utilized to track the time for the process and each time must and should have different names but the time taken for a particular process can be known by using the same name with the help of the command console.timeEnd(). console.timeEnd(label) - Using the function console.timeEnd() is utilized to end the timer for the process initialized by console.time() function. The example below demonstrate the use of console.timeEnd() function. timeEnd.js [c] console.time('Process timer'); var sum = 0; for (var x=0; x<200000; x++) { sum += x; } console.log('Time taken Sum up numbers from 1 to 200000'); console.timeEnd('Process timer'); [/c] Compiling the above code will produce the output as shown in the image below. console.log([data][, …arg]) - The function console.log() is utilized to print with newline to stdout, the image below gives the example for utilizing the console.log() function. The same example can be executed by creating .js file with the code. log.js [c]console.log(“SPLessons Online Tutorials”)[/c] By compiling the code, one can get the output as shown in the image below. console.error([data][, …arg]) - The function console.error() method is similar to console.log() but the output here sent to stderr instead of stdout. This function is utilized for error messages and more use of this method can slow down the process. Multiple arguments can be passed and the first argument is used as the first message and the rest used as additional values. The snippet code below demonstrates the console.error() function. Create a .js file with the following code. err.js [c] const code = 10; console.error('error', code); console.error('error #%d', code); [/c] By compiling the code, one can get the output as shown in the image below. console.warn([data][, …arg]) - The function console.warn() is similar to console.error(), the example below demonstrates the code for warn function which print a message when a specific file opened and print a warning message if the specified file not found. [c] var fd = require("fs"); var path = "text1.txt"; fd.open(path, "r", function(err, fd){ if (err){ console.warn(err); } else{ console.log("File open successfully" + path); } }) [/c] By compiling the code, one can get the output as shown in the image below. console.assert(expression, [message]) - The function console.assert() is utilized to check the expression wither true or false, the image below demonstrates an example for console.assert() function. console.dir(obj[, options]) - The function console.dir(obj[,option]) utilize the util.inspect on obj and then prints the string to stdout.

Summary

shape Key Points

  • The Node.js console is a global object
  • The Node.js console logging have few built-in console object for printing the stderr and stdout.
  • Using the REPL command prompt it is easy to execute the raw JavaScript code.
  • The Node.js REPL can be accessible for both as a standalone program or can be included in different programming applications.