JavaScript Tip: Bust and Disable console.log

Here’s a quick and dirty follow-up to my original Save me from console.log errors. The main improvement to this version is that it includes a way to disable console.log (and related functions), for example, in production environments.

While console.log is awesome, you really don’t want your dirty inner workings littering up the Console (or visible to the user — though, you could make an argument that this is a great way to debug errors in live environments) once your app is deployed.

Note: Firebug can get unhappy sometimes if you try to mess with its console object. But, in theory, this approach should work.


var DEBUG_MODE = true; // Set this value to false for production

if(typeof(console) === 'undefined') {
    console = {}
}

if(!DEBUG_MODE || typeof(console.log) === 'undefined') {
    // FYI: Firebug might get cranky...
    console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}

Leave a Reply