Last active
January 12, 2017 22:56
-
-
Save positlabs/4073517 to your computer and use it in GitHub Desktop.
Disables console logging for production builds
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
(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