clim

Console.Log IMproved

npm install clim
21 downloads in the last day
81 downloads in the last week
388 downloads in the last month

Console.Log IMproved - clim

A little Node.js module which improves the behavior of the logging methods of the console object without changing its API. Just drop it in.

Improvements

Improvements affect only log, info, warn and error methods.

  • Add timestamp
  • Add log level LOG/INFO/WARN/ERROR
  • Always log to stderr
  • Allow prefixing and inheriting

Installation

npm install clim

Usage

Function Signature

Object newconsole = clim( [String prefix], [Object parent], [Boolean patch parent] )

All parameters are optional.

Just shadow the original console object and use it like always:

var console = require("clim")();
console.log("message");
console.info("message");
console.warn("message");
console.error("message");

Or if you want process wide improved console object you can monkeypatch the original object by passing it and true to clim:

require("clim")(console, true);
console.log("message");

Prefix Inheriting

Add prefix to your log messages by passing it as the first argument:

var console = require("clim")("myapp");
console.log("message");
Sun Sep 30 2012 16:45:57 GMT+0300 (EEST) INFO myapp message

Inherit prefixes from some other console object by passing it as the second parameter:

var clim = require("clim");

var console = clim("myapp");
console.log("message");

function somefunc(){
  var logger = clim("somefunc", console);
  logger.warn("in function");
}

somefunc();
Sun Sep 30 2012 16:59:12 GMT+0300 (EEST) INFO myapp message
Sun Sep 30 2012 16:59:12 GMT+0300 (EEST) WARNING myapp somefunc in function

Customizing

Change date format by overriding getTime:

clim.getTime = function(){
  return new Date().toString();
};

Change global log target and formatting details by overriding logWrite:

clim.logWrite = function(level, prefixes, msg) {
  // Default implementation writing to stderr
  var line = clim.getTime() + " " + level;
  if (prefixes.length > 0) line += " " + prefixes.join(" ");
  line += " " + msg;
  process.stderr.write(line + "\n");

  // or post it web service, save to database etc...
};

Design Philosophies

  • Keep the same api as in the original console object
  • Small
  • No dependecies
  • Tests
  • MIT Licensed
npm loves you