Esa-Matti Suuronen

I write code and jump

Improving console.log for Node.js

Node.js gives you four methods for logging stuff console.log(), console.info(), console.warn(), and console.error(). These could go pretty far, but unfortunately they fall bit short since their output does not give any indication which method was used for logging except for the output stream. Which in my opinion is bit confusing. But I do like their API. They can do printf like formating, pretty printing objects and they can even handle objects with circular references.

So I’ve written a little module called clim (Console.Log IMproved) which gives some superpowers to console.log and friends.

Usage

You can shadow the original console or monkeypatch it once and for all

1
2
3
4
5
6
7
var clim = require("clim");

// Just shadow it
var console = clim();

// or monkeypatch it!
clim(console, true);

Now you can use console.log just like before, but the output is more detailed:

1
2
3
4
console.log("message with log");
console.info("message with info");
console.warn("message with warn");
console.error("message with error");
1
2
3
4
Sun Sep 30 2012 23:06:24 GMT+0300 (EEST) LOG message with log
Sun Sep 30 2012 23:06:24 GMT+0300 (EEST) INFO message with info
Sun Sep 30 2012 23:06:24 GMT+0300 (EEST) WARN message with warn
Sun Sep 30 2012 23:06:24 GMT+0300 (EEST) ERROR message with error

Also now all the methods write to stderr for the sake of consistency. By default log and info writes to stdout and warn and error to stderr, but this causes some pains when redirecting logs to a single file. Log order might be messed up depending on IO buffering etc.

Of course keep in mind that this might break your unix style app which uses stdout to communicate outside if you depend on console.log() being the print statement of Node.js. Rather use process.stdout.write(). It is more explicit and gives you more control.

Background

The main idea behind this module is to keep the original API of console.log(), because then it is possible to just drop it in a project without any refactoring. If you encouter any inconsistencies with with it please file a bug.

clim also exposes few hooks that can be used to customize its behaviour. You can modify the date string, change log target from stderr to back stdout or even to a database, add default prefixes to console objects and inherit from them. For more details view the project page on Github:

https://github.com/epeli/node-clim

Comments