Making Logs Colorful in NodeJS

Prateek Singh
Feb 3 · 3 min read
Image Credits: Bapu Graphics

Logging is a very important part of any application. It helps us to debug the issues, display important stats using Splunk & a lot more. From the very start of our coding days, Logs are our true friends and helps us a lot. So basically Logs are one of the most required aspect of any server-side code architecture. There are many logging libraries available in the market like Winston, Loggly, Bunyan, etc. But when it comes to debugging our APIs or we need to check the value of some variable we simply call our best friend in JavaScript console.log(). Let’s check some of the examples, How generally people put logs in their codes.

console.log("MY CRUSH NAME");
console.log("AAAAAAA");
console.log("--------------------");
console.log("Step 1");
console.log("Inside If");

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.

Image 1

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.

Colorful Logs

Image 2

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…

Image 3

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.

Install

npm install chalk

Usage

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!'));
console.log(warning('Warning!'));

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.

Modifier

  • 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.

Colors

  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white
  • blackBright (alias: gray, grey)
  • redBright
  • greenBright
  • yellowBright
  • blueBright
  • magentaBright
  • cyanBright
  • whiteBright

Thanks for reading the article. In the future, I will update you with some of the less-known tricks and tips of JavaScript which will make your development quite easy.

Happy Coding || Write to Learn

Frontend Weekly

It's really hard to keep up with all the front-end…

Prateek Singh

Written by

Talk is Cheap… SHOW ME THE CODE.

Frontend Weekly

It's really hard to keep up with all the front-end development news out there. Let us help you. We hand-pick interesting articles related to front-end development. You can also subscribe to our weekly newsletter at http://frontendweekly.co

More From Medium

More from Frontend Weekly

More from Frontend Weekly

More from Frontend Weekly

Understanding `static` in React

4.3K

More from Frontend Weekly

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade