Make Node.js debugging easier with colorful log messages

Image for post
Image for post
Request, queries run, and the response — in that order.

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('Request : ' + req.method + ': ' + req.originalUrl));
console.log('Request Headers: ', JSON.stringify(req.headers)));
console.log('Request Params: ', JSON.stringify(req.params)));
console.log('Request Query: ', JSON.stringify(req.query)));
console.log('Request Body: ', JSON.stringify(req.body)));

Log responses

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

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:

Image for post
Image for post
Messages in red indicate something went wrong. In this case, the requested resource wasn’t found.

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.

Coding, machine learning, reading, sleeping, listening, potato. and

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store