Skip to content

Instantly share code, notes, and snippets.

@positlabs
Last active January 12, 2017 22:56
Show Gist options
  • Save positlabs/4073517 to your computer and use it in GitHub Desktop.
Save positlabs/4073517 to your computer and use it in GitHub Desktop.
Disables console logging for production builds
(function(){
var noop = function noop(){};
var liveConsole = console;
var consoleMethodNames = 'assert assert clear count debug dir dirxml error group groupCollapsed groupEnd info log markTimeline profile profileEnd table time timeEnd timeStamp timeline timelineEnd trace warn'.split(' ');
var deadConsole = {};
for (var i = consoleMethodNames.length - 1; i >= 0; i--) {
deadConsole[consoleMethodNames[i]] = noop;
};
window.__debug = function (enabled){
if(enabled){
window.console = liveConsole;
}else{
window.console = deadConsole;
}
}
})();
/////////////////
// example usage
////////////////
var shouldConsoleLog = true;
if(window.location.href.match('your-production-url.com') !== null){shouldConsoleLog = false;} // disable for production mode
if(window.location.href.match('debug') !== null){shouldConsoleLog = true;} // if debug is found anywhere in url, then log stuff.
window.__debug(shouldConsoleLog);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment