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 |
|
Now you can use console.log
just like before, but the output is more detailed:
1 2 3 4 |
|
1 2 3 4 |
|
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: