Skip to content

Instantly share code, notes, and snippets.

@lynxerzhang
Last active June 12, 2017 09:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lynxerzhang/7b0f9108cd14c333b82390c179f804a9 to your computer and use it in GitHub Desktop.
Save lynxerzhang/7b0f9108cd14c333b82390c179f804a9 to your computer and use it in GitHub Desktop.
console.log and console.warn's Polyfill
//@see https://www.paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
//inspired from paulirish's console.log function
!(function(win, funNameAry, titleAry, max){
max = max || 100;
for(var i = 0, funName = ""; i < funNameAry.length, funName = funNameAry[i]; i ++){
win[funName] = (function(i, n){
return function(){
this[n].history = this[n].history || [];
this[n].logMax = this[n].logMax || max;
this[n].history.push(arguments);
if(this[n].history.length > this[n].logMax){
this[n].history = this[n].history.slice(-this[n].logMax);
}
if(win.console && typeof win.console[n] === "function"){
this[n].logbind = this[n].logbind || this.console[n].bind(this.console, "[" + titleAry[i] + "]");
this[n].logbind.apply(null, Array.prototype.slice.call(arguments));
}
}
})(i, funName);
}
}(window, ["log", "warn"], ["trace", "warning"]));
//usage:
log("trace to console");
warn("warning for something");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment