Skip to content

Instantly share code, notes, and snippets.

@TanmayChakrabarty
Last active January 1, 2019 20:36
Show Gist options
  • Save TanmayChakrabarty/90b07220bb01fac00317eec7b797be18 to your computer and use it in GitHub Desktop.
Save TanmayChakrabarty/90b07220bb01fac00317eec7b797be18 to your computer and use it in GitHub Desktop.
Enable Disable Javascript Console Output easily for development and production
var clogger = {
consoleMethods: ['log','error','warn','info'],
consoleStoredMethods: {},
initiated: false,
init: function(){
if(!this.initiated){
for(var i=0; i<this.consoleMethods.length; i++){
this.consoleStoredMethods[this.consoleMethods[i]] = window['console'][this.consoleMethods[i]];
window['console'][this.consoleMethods[i]] = function(){};
}
this.initiated = true;
}
},
reset: function(){
if(this.initiated){
for(var i=0; i<this.consoleMethods.length; i++){
window['console'][this.consoleMethods[i]] = this.consoleStoredMethods[this.consoleMethods[i]];
}
this.consoleStoredMethods = {};
this.initiated = false;
}
},
enableLoggingOn: function(logMethods){
this.init();
if(typeof logMethods === 'undefined') logMethods = null;
if(logMethods && !(logMethods instanceof Array)) logMethods = [logMethods];
methodsToEnable = logMethods ? logMethods : this.consoleMethods;
for(var i=0;i<methodsToEnable.length; i++){
var thsMethod = methodsToEnable[i];
window['console'][thsMethod] = this.consoleStoredMethods[thsMethod];
}
},
disableLoggingOn: function(logMethods){
this.init();
if(typeof logMethods === 'undefined') logMethods = null;
if(logMethods && !(logMethods instanceof Array)) logMethods = [logMethods];
methodsToDisable = logMethods ? logMethods : this.consoleMethods;
for(var i=0;i<methodsToDisable.length; i++){
var thsMethod = methodsToDisable[i];
window['console'][thsMethod] = function(){};
}
}
};
@TanmayChakrabarty
Copy link
Author

TanmayChakrabarty commented Jan 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment