console.log("MY CRUSH NAME");
Why we put logs like this? Is it because we are lazy? No, we put logs like this because we need to differentiate it from other logs printing on the console.
For the moment we need only currently added console.log(“Got the packets”) on the terminal and not other logs. Are you able to see the “Got the packets” printed in the logs(Image 1)? I know its difficult to figure out the log. So what to do? How we can make our life easy and logs beautiful.
If I tell you that, these logs can be printed in different & multiple colors at the same time. Life would be much easier, right? Let’s take a look at the next image and try to find the same log “Got the packets”.
“Got the packets” is now clearly visible in Red color. Isn’t it great? We can put different logs in different colors. I bet this is gonna change your logging style and make it a lot easier. See one more example…
The newly added log is clearly visible. Now let’s take a look at the implementation of this functionality. We can achieve this by adding the Chalk module into our code.
npm install chalk
const chalk = require('chalk');
console.log(chalk.blue('Hello world!'));//Print String in Blue Color
You can also customize your own theme and use it like this.
const chalk = require('chalk');const error = chalk.bold.red;const warning = chalk.keyword('orange');console.log(error('Error!'));
So basically it's like chalk[MODIFIER][COLOR] & we are good to go to print colorful logs in our code 😊. “Chalk” module gives us numbers of modifiers and colors to print in.
reset- Resets the current color chain.
bold- Make text bold.
dim- Emitting only a small amount of light.
italic- Make text italic. (Not widely supported)
underline- Make text underline. (Not widely supported)
inverse- Inverse background and foreground colors.
hidden- It prints the text but makes it invisible.
strikethrough- Puts a horizontal line through the center of the text. (Not widely supported)
visible- Prints the text only when Chalk has a color level > 0. It can be useful for things that are purely cosmetic.
Happy Coding || Write to Learn