Skip to content

Instantly share code, notes, and snippets.

@sr3d
Created December 5, 2011 19:27
Show Gist options
  • Save sr3d/1434897 to your computer and use it in GitHub Desktop.
Save sr3d/1434897 to your computer and use it in GitHub Desktop.
UberLogger
(function() {
if(typeof(console) == 'undefined') {
(function() {
/* Shim for window.console */
var methods = ['log', 'info', 'warn', 'error'];
window.console = {};
for(var i = 0; i < methods.length; i++){
window.console[ methods[i] ] = function() {};
}
})();
/* Load up firebug lite */
if(/debug/i.test(window.location.hash)) {
var script = document.createElement('script');
script.setAttribute("type", "text/javascript");
script.setAttribute("src", "https://getfirebug.com/firebug-lite.js#startOpened");
(document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script);
};
};
var Logger = {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
level: 3 // default to WARN
,_log: function(level) {
if(level >= this.level) {
/* console.log/info/warn/error doesn't have apply method. */
Function.prototype.apply.call(
console[ level == 0 ? 'log' :
level == 1 ? 'info' :
level == 2 ? 'warn' :
level == 3 ? 'error' :
'debug'],
window.console, Array.prototype.slice.call(arguments, 1)[0] );
}
return;
}
,log: function() { this._log.call(this, this.level, arguments); }
,debug: function() { this._log.call(this, this.DEBUG, this._addLogLevel('DEBUG', arguments)); }
,info: function() { this._log.call(this, this.INFO, this._addLogLevel('INFO ', arguments)); }
,warn: function() { this._log.call(this, this.WARN, this._addLogLevel('WARN ', arguments)); }
,error: function() { this._log.call(this, this.ERROR, this._addLogLevel('ERROR', arguments)); }
,_addLogLevel: function(level, msg) {
typeof(msg[0]) == 'object' ?
Array.prototype.splice.call(msg, 0, 0, level + " %o") :
msg[0] = level + " " + msg[0];
return msg;
}
};
window.logger = Logger;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment