views:

958

answers:

4

Is there any way to turn off all console.log statements in my JavaScript code, for testing purposes?

Thanks!

+5  A: 
SolutionYogi
That doesn't work.
Zachary Burt
Please provide detail as to what doesn't work? Does the above line give you an error? If yes, what's the error message?
SolutionYogi
Works for me in IE8. ;-)
Eugene Lazutkin
Excellent, thanks. Do you know if this works in IE7?
Zachary Burt
The code overwrites and restores console.log function. If IE7 supports console.log method, it should work.
SolutionYogi
console.log = function() {} does not seem to work in Firefox. You still get a 'console is not defined' error.
DA
+5  A: 

As far as I can tell from the documentation, Firebug doesn't supply any variable to toggle debug state. Instead, wrap console.log() in a wrapper that conditionally calls it, i.e.:

DEBUG = true; // set to false to disable debugging
function debug_log() {
    if ( DEBUG ) {
        console.log.apply(this, arguments);
    }
}

To not have to change all the existing calls, you can use this instead:

DEBUG = true; // set to false to disable debugging
old_console_log = console.log;
console.log = function() {
    if ( DEBUG ) {
        old_console_log.apply(this, arguments);
    }
}
Cide
Thanks, although this means I need to rewrite all my console.log statements as debug.log.
Zachary Burt
Updated my example to provide the best of two worlds. :)
Cide
This is the right way to do it - ofcourse if you are starting from scratch.
OpenSource
It is also the right way to do it if you have a good find/replace function in your editor.
BaroqueBobcat
No need to write your own wrapper, btw, at least if you're using jQuery. The jQuery Debugging Plugin works great. As a bonus it provides emulation of console.log on browsers without it.http://trainofthoughts.org/blog/2007/03/16/jquery-plugin-debug/
Nelson
The only [minor] problem, of course, being that you need to install a plugin. :) Good to know, though - thanks!
Cide
well not install, rather include the plugin on your page
redsquare
Good idea, but it doesn't work everywhere: IE8 defines console.log, but doesn't define console.log.apply. Bummer.
Eugene Lazutkin
Doesn't look like that second option works in <=IE7. You get a 'console is not defined' error in those browsers.
DA
+3  A: 

The following is more thorough:

var DEBUG = false;
if(!DEBUG){
    if(!window.console) window.console = {};
    var methods = ["log", "debug", "warn", "info"];
    for(var i=0;i<methods.length;i++){
     console[methods[i]] = function(){};
    }
}

This will zero out the common methods in the console if it exists, and they can be called without error and virtually no performance overhead. In the case of a browser like IE6 with no console, the dummy methods will be created to prevent errors. Of course there are many more functions in Firebug, like trace, profile, time, etc. They can be added to the list if you use them in your code.

You can also check if the debugger has those special methods or not (ie, IE) and zero out the ones it does not support:

if(window.console && !console.dir){
var methods = ["dir", "dirxml", "trace", "profile"]; //etc etc
    for(var i=0;i<methods.length;i++){
     console[methods[i]] = function(){};
    }
}
mwilcox
A: 

I'm havin a problem with the console error, were is this progrm so I can just remove it. Thanks

Scoutmagazine