I’m currently developing a node.js based application and I need to deploy it on a cluster where I can’t obtain console logs from. While I develop and test on my local computer, there are always things that can go wrong on the cluster (for example caused by differences in software versions).

So I quickly threw some components together that allowed me to follow my node.js app logs in real time. Nothing special at all, but possibly interesting for someone who wants to do the same.

The first thing needed is a way to intercept all output by my application. Luckily there is already a node.js module existing for this, intercept-stdout. Easily installed using npm:

Next up we need a way to send the logs in real time to a client. What could be more simple than socket.io?

As my application doesn’t generate a lot of logs, I’m going to store them in a simple array. This is most likely not optimal for a lot of applications and you might want to limit the array size or store the data elsewhere (redis might be an option).

I added the following code at the top of my main javascript file (before importing any modules) :

I simply intercept all data, push it into an array and emit it if the io connection is existing.

The following code is added after the init of my Express server:

When a connection is made it will send all previous logs to the client.

Naturally we now need a frontend to show the logs.

Using ansi_up (bower install ansi_up) for ansi coloring and jQuery you now have a pretty console that allows you to follow in real time the output of your node.js application.