Created
February 2, 2013 19:32
-
-
Save birla/4698932 to your computer and use it in GitHub Desktop.
Backbone Application Console Logging (uses underscore.js)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| App.Log = { | |
| level: 0, | |
| _ie: ( | |
| function(){ | |
| var undef, v = 3, | |
| div = document.createElement('div'), | |
| all = div.getElementsByTagName('i'); | |
| while (div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->', all[0] ); | |
| return v > 4 ? v : undef; | |
| }() | |
| ), | |
| _av_funcs: ['error','info','log','log','log'], | |
| _log: function() { | |
| if(!console) return; | |
| var args = Array.prototype.slice.call(arguments), | |
| func=args.shift(), level=args.shift(); | |
| if(typeof console[func] === 'function' && level<=this.level) { | |
| if(this._ie) { | |
| for(var i = 1, len = args.length; i < len; i++) { | |
| console[func].apply(console, [args[0], " ", args[i]]); | |
| } | |
| } else { | |
| console[func].apply(console, args); | |
| } | |
| } | |
| return; | |
| }, | |
| init: function(level) { | |
| if(level) this.level = level; | |
| if (Function.prototype.bind && console && typeof console.log == "object") { | |
| (function () { | |
| _.each(["log","info","error"], | |
| function (method) { | |
| if(console[method]) { | |
| console[method] = Function.prototype.call.bind(console[method], console); | |
| } else { | |
| var i=-1; | |
| while((i = _.indexOf(this._av_funcs, method)) !== -1) { | |
| this._av_funcs[i] = 'log'; | |
| } | |
| } | |
| }, | |
| this | |
| ); | |
| })(); | |
| } | |
| return; | |
| }, | |
| _call: function(caller, args, type) { | |
| args = Array.prototype.slice.call(args); | |
| if(typeof type !== 'undefined') { | |
| try {args = type.concat(args);} catch (e) {} | |
| } | |
| return caller.apply(this, args); | |
| }, | |
| verb: function() { | |
| return this._call(this._log, arguments, [this._av_funcs[4], 5,'VERB']); | |
| }, | |
| xhr: function() { | |
| return this._call(this._log, arguments, [this._av_funcs[3], 4,'XHR']); | |
| }, | |
| debug: function() { | |
| return this._call(this._log, arguments, [this._av_funcs[2], 3,'DEBUG']); | |
| }, | |
| info: function() { | |
| return this._call(this._log, arguments, [this._av_funcs[1], 2,'INFO']); | |
| }, | |
| error: function() { | |
| return this._call(this._log, arguments, [this._av_funcs[0], 1,'ERROR']); | |
| } | |
| }; | |
| App.Log.init(4); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment