Wynn Netherland changelog.com/posts

andlog - Simple client-side console switch for web apps

A small yet useful script from Henrik Joreteg to conditionally turn on client-side debugging in web apps. Instead of having to wrap console.log to include a conditional, Henrik looks for a debug flag in localStorage:

// follow @HenrikJoreteg and @andyet if you like this ;)
(function () {
if (localStorage && localStorage.debug && window.console) {
    if (typeof exports !== 'undefined') {
        module.exports = window.console;
    } else {
        // overwrite it... yeah, weird, but it works.
        window.console = window.console;
    }
} else {
    var methods = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),
        l = methods.length,
        i = 0,
        res = {},
        fn = function () {};

    for (; i < l; i++) {
        res[methods[i]] = fn;
    }

    if (typeof exports !== 'undefined') {
        module.exports = res;
    } else {
        window.console = res;
    }
}
})();

Check out the README for usage and CommonJS support details.


Discussion

Sign in or Join to comment or subscribe

Player art
  0:00 / 0:00