Introduction
Printing to the command line console is essential for debugging and monitoring purposes in Node.js. In this blog, we will explore various ways to output messages to the command line using Node, starting from basic output with console.log
to more advanced scenarios like formatting, clearing the console, counting elements, printing stack traces, measuring time, coloring the output, and creating a progress bar.
Let’s dive in!
Basic output using the console module
Node provides a console
module that offers several methods to interact with the command line. The most basic and commonly used method is console.log()
, which prints the string or object passed to it to the console.
Here is an example that demonstrates the usage of console.log()
:
const x = 'x'
const y = 'y'
console.log(x, y)
To format dynamic phrases, you can use format specifiers like %s
, %d
, %f
, and %O
. For example:
console.log('My %s has %d years', 'cat', 2)
For more information on the available format specifiers, refer to the official documentation.
Now let’s explore some other useful methods provided by the console
module.
Clear the console
To clear the console, you can use the console.clear()
method. However, please note that the behavior of this method may vary depending on the console being used.
Counting elements
You can count the number of times a specific message or variable is printed using the console.count()
method. This can be useful for tracking the frequency of certain events during debugging or logging.
Here is an example:
const x = 1
const y = 2
const z = 3
console.count('The value of x is ' + x + ' and has been checked .. how many times?')
console.count('The value of x is ' + x + ' and has been checked .. how many times?')
console.count('The value of y is ' + y + ' and has been checked .. how many times?')
Print the stack trace
Sometimes, it’s helpful to print the call stack trace of a function for debugging purposes. You can achieve this using the console.trace()
method.
Here is an example:
const function2 = () => console.trace()
const function1 = () => function2()
function1()
The stack trace will provide information about how different functions were called, helping you understand the flow of execution.
Calculate the time spent
You can measure the execution time of a function using the console.time()
and console.timeEnd()
methods. These methods allow you to calculate the time taken to run a specific block of code.
Here is an example:
const doSomething = () => console.log('test')
const measureDoingSomething = () => {
console.time('doSomething()')
//do something, and measure the time it takes
doSomething()
console.timeEnd('doSomething()')
}
measureDoingSomething()
The output will display the elapsed time between the console.time()
and console.timeEnd()
statements.
stdout and stderr
By default, console.log()
prints messages to the standard output stream (stdout
). However, you can also print error messages to the standard error stream (stderr
) using the console.error()
method. These error messages will be logged in the error log rather than the console.
Color the output
You can add color to your console output using escape sequences. An escape sequence is a set of characters that represents a specific color.
Here’s an example of how to print colored text in the console:
console.log('\x1b[33m%s\x1b[0m', 'hi!')