Last active
January 1, 2019 20:36
-
-
Save TanmayChakrabarty/90b07220bb01fac00317eec7b797be18 to your computer and use it in GitHub Desktop.
Enable Disable Javascript Console Output easily for development and production
This file contains 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
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(){}; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read uses documentation at
https://www.onlineclassnotes.com/2018/09/enable-disable-javascript-console-output-easily-for-development-and-production.html