Create a gist now

Instantly share code, notes, and snippets.

@ciju /colorlog.js
Last active Dec 19, 2015

Embed
var App = App || {};
(function (app) {
var SCHEMES = ['green:white',
'yellow:black',
'blue:white',
'red:white'];
// if string param, prepend it in all logs. if not, the usual
// color shoudl work. Check if first param is string, and add it.
function fmtType(arg) {
if (typeof arg == 'string') {
return '%s';
}
return '%o';
}
function fmtString(args) {
var fmtArr = [];
for (var i=0; i<args.length; i++) {
fmtArr.push(fmtType(args[i]));
}
return fmtArr.join(' ');
}
function colorStr(bg, fg, args) {
return ["%c" + fmtString(args), "background-color:"+bg+';color:'+fg+';padding:1px;'];
};
function colorLog(bg, fg, args) {
var fmt = colorStr(bg, fg, args);
console.log.apply(console, fmt.concat(args));
};
function addPrefixToArgs(str, args) {
args = Array.prototype.slice.call(args);
return [str].concat(args);
}
function generateLogger(prefix) {
var fn = function () {
console.log.apply(console, addPrefixToArgs(prefix, arguments));
};
SCHEMES.forEach(function (e) {
var bg = e.split(':')[0],
fg = e.split(':')[1];
fn[bg] = function () {
colorLog(bg, fg, addPrefixToArgs(prefix, arguments));
};
});
return fn;
}
function instrument_fns(proto, cb) {
var i;
function instrument_fn(fn, name, h) {
if (typeof fn != 'function') return;
h[name] = function () {
var args = Array.prototype.slice.call(arguments);
cb.call(this, name, args, this);
return fn.apply(this, args);
};
}
for (i in proto) {
log.red("fn", i);
instrument_fn(proto[i], i, proto);
}
}
var log = generateLogger('');
log.withPrefix = function (name) {
if (typeof name != 'string') {
return log;
}
return generateLogger(name);
};
log.noop = function () {
return function () {};
};
log.mute = function () {
var fn = log.noop();
SCHEMES.forEach(function (e) {
var bg = e.split(':')[0],
fg = e.split(':')[1];
fn[bg] = log.noop();
});
return fn;
};
log.instrument_fns = instrument_fns;
app.log = log;
}(App));
// Usage:
App.log("some", {obj:1}, "somethingelse");
App.log.green("some", {obj:1}, "somethingelse");
var mlog = App.log.withPrefix("MODULE:");
mlog("some", {obj:1}, "somethingelse");
mlog.green("some", {obj:1}, "somethingelse");
// i prefer
(function (app, log) {
log("initializing");
// your app logic etc.
log.green("something interesting");
}(App, App.log.withPrefix("myModule:")))
// that way, i can mute logging for IIF by
(function (app, log) {
log("initializing");
// your app logic etc.
log.green("something interesting");
}(App, App.log.mute()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment