You are here
Home > Tech >

Make Node.js debugging easier with colorful log messages

I’m working on my second project in Node.js. I kind of love it. I haven’t yet decided to find out how better it performs compared to apps written in other languages, such as PHP, or Java. But writing Node.js apps is fun, mostly because you can get npm packages for almost everything you need during development. This helps you concentrate on the business logic and not worry about writing basic boilerplate code. What’s the advantage? Well, you can write a POC/MVP app really quickly.

Anyway, one thing I noticed while working on the previous Node.js app was that debugging is a bit difficult if you, like me, have an obsession with having proper log messages everywhere. So one of the first things I did when I was setting up this project was write a couple of middleware methods to log all the requests coming in to the app and the responses going out from the app. This way, when I start writing the clients for this API app, I can just tail the log file and get all the info I need. Plus, in the future as well, debugging this app is going to be super easy if you have the requests and responses logged.

It’ll become a bit difficult to figure out where your request starts and where your response starts, in the log file. So I thought I’ll add some colors to the log messages so that it’s becomes easy to figure out which is the request, which is the response, and which are the intermediate log messages.

I did one simple Google search and I got three packages which can add color to your log messages. I decided to go with the one which looked the most easiest and the most basic. The npm package I selected is simply called, colors. To get it into your app, just run:

npm install --save colors

The “save” option in the command will automatically add this package to your “package.json” file, to make life that much easier.

Now that you have the package installed, you can start logging colorful text in the console. The npm page of the package gives you all the examples you’d ever want. But here’s a snipped of what I’ve written in my middleware methods to get the log you saw in the screenshot above.

Log requests

console.log(colors.black.bgYellow('----------------'));
console.log(colors.black.bgYellow('Request : ' + req.method + ': ' + req.originalUrl));
console.log(colors.black.bgYellow('Request Headers: ', JSON.stringify(req.headers)));
console.log(colors.black.bgYellow('Request Params: ', JSON.stringify(req.params)));
console.log(colors.black.bgYellow('Request Query: ', JSON.stringify(req.query)));
console.log(colors.black.bgYellow('Request Body: ', JSON.stringify(req.body)));
console.log(colors.black.bgYellow('---------'));

Log responses

if(JSON.parse(body).status == 1) {
 console.log(colors.green('*********'));
 console.log(colors.green('Response Body: ', body));
 console.log(colors.green('*********'));
} else {
 console.log(colors.red('***********'));
 console.log(colors.red('Response Body: ', body));
 console.log(colors.red('***********'));
}

I add a “status” flag to each of my responses. So based on that flag, I’ll be able to figure out if the request operation was a success or not (I change the HTTP status of the response as well — 401, 404, 422, 200, etc.). So if “status == 1” the response will be logged in green, else, it’ll be logged in red, as shown in the screenshot below:

NodeJSLog

There’s a lot more you can do with this simple little package and your console. What I’m doing here doesn’t look like much. But it makes looking at log messages so much easier, at least to me.

Let me know if there’s anything else that would make debugging a Node.js app easy.

PS: The other two packages I found which let you do the same are cli-color and chalk.

Sunny Srinidhi
Coding, reading, sleeping, listening, watching, potato. INDIAN. "If you don't have time to do it right, when will you have time to do it over?" - John Wooden
https://blog.contactsunny.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Top