Skip to content

Instantly share code, notes, and snippets.

@dtan
Created March 29, 2012 15:59
Show Gist options
  • Save dtan/2238923 to your computer and use it in GitHub Desktop.
Save dtan/2238923 to your computer and use it in GitHub Desktop.
Custom javascript logger
(function (undefined) {
// handles all the console methods to make the console persistent, mainly for IE
if (window.console === undefined) {
window.console = {};
}
(function () {
var methods = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"],
i = methods.length;
for ( ; i--; ) {
if ( !window.console[ methods[i] ] ) {
window.console[ methods[i] ] = function () {};
}
}
}());
}());
(function(window) {
var qs = {};
function logger() {
var s = window.location.search.substring(1),
ss = s.split('&'),
i = ss.length;
while (i--) {
var si = ss[i],
sp = si.split('=') ,
l = sp[0],
r = sp[1];
qs[l] = r;
}
}
logger();
var log = function () {
if (qs.debug === 'true') {
var args = Array.prototype.slice.call(arguments),
arg0 = args[0],
is_method = !!window.console[arg0],
method = is_method ? window.console[arg0] : window.console.log,
args_to_apply = is_method ? args.slice(1) : args;
console.log(args);
method.apply(window.console, args_to_apply)
}
};
// to expose this logger to the global namespace:
// window.log = log;
}(window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment