How to quickly and conveniently disable all console.log statements in my code?

0 votes
asked Jul 31, 2009 by zachary-burt

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

21 Answers

0 votes
answered Jul 1, 2009 by mwilcox

The following is more thorough:

var DEBUG = false;
    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(){};
0 votes
answered Jul 31, 2009 by solutionyogi

Redefine the console.log function in your script.

console.log = function() {}

That's it, no more messages to console.


Expanding on Cide's idea. A custom logger which you can use to toggle logging on/off from your code.

From my Firefox console:


var logger = function()
    var oldConsoleLog = null;
    var pub = {};

    pub.enableLogger =  function enableLogger() 
                            if(oldConsoleLog == null)

                            window['console']['log'] = oldConsoleLog;

    pub.disableLogger = function disableLogger()
                            oldConsoleLog = console.log;
                            window['console']['log'] = function() {};

    return pub;


        console.log('hi', 'hiya');
        console.log('this wont show up in console');

        console.log('This will show up!');

How to use the above 'logger'? In your ready event, call logger.disableLogger so that console messages are not logged. Add calls to logger.enableLogger and logger.disableLogger inside the method for which you want to log messages to the console.

0 votes
answered Jul 31, 2009 by cide

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);
0 votes
answered Jul 20, 2011 by chris-s

If you're using IE7, console won't be defined. So a more IE friendly version would be:

if (typeof console == "undefined" || typeof console.log == "undefined") 
   var console = { log: function() {} }; 
0 votes
answered Jul 22, 2011 by scott-greenfield

I know you asked how to disable console.log, but this might be what you're really after. This way you don't have to explicitly enable or disable the console. It simply prevents those pesky console errors for people who don't have it open or installed.

if(typeof(console) === 'undefined') {
    var console = {};
    console.log = console.error = = console.debug = console.warn = console.trace = console.dir = console.dirxml = = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
0 votes
answered Jan 31, 2012 by stijn-geukens

You could use javascript AOP (e.g. jquery-aop) to intercept all calls to console.debug/log (around) and do not proceed with the actual invocation if some global variable is set to false.

You could even do an ajax call (now and then) so you can change the log enabled/disabled behavior on the server which can be very interesting to enable debugging when facing an issue in a staging environment or such.

0 votes
answered Jul 2, 2012 by tom-mckearney

Warning: Shameless plug!

You could also use something like my JsTrace object to have modular tracing with module-level "switching" capability to only turn on what you want to see at the time.

(Also has a NuGet package, for those who care)

All levels default to "error", though you can shut them "off". Though, I can't think of why you would NOT want to see errors

You can change them like this:

Trace.traceLevel('ModuleName1', Trace.Levels.log);

Fore more docs, check out the Documentation


0 votes
answered Jan 14, 2013 by kikeenrique

I found a little more advanced piece of code in this url JavaScript Tip: Bust and Disable console.log:

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.debug = console.warn = console.trace = console.dir = console.dirxml = = console.groupEnd = console.time =    console.timeEnd = console.assert = console.profile = function() {};
0 votes
answered Jul 8, 2013 by justin

This a hybrid of answers from SolutionYogi and Chris S. It maintains the console.log line numbers and file name. Example jsFiddle.

// Avoid global functions via a self calling anonymous one (uses jQuery)
(function(MYAPP, $, undefined) {
    // Prevent errors in browsers without console.log
    if (!window.console) window.console = {};
    if (!window.console.log) window.console.log = function(){};

    //Private var
    var console_log = console.log;  

    //Public methods
    MYAPP.enableLog = function enableLogger() { console.log = console_log; };   
    MYAPP.disableLog = function disableLogger() { console.log = function() {}; };

}(window.MYAPP = window.MYAPP || {}, jQuery));

// Example Usage:
$(function() {    
    console.log('this should not show');

    console.log('This will show');
0 votes
answered Jul 9, 2013 by joey-schooley

I realize this is an old post but it still pops up at the top of Google results, so here is a more elegant non-jQuery solution that works in the latest Chrome, FF, and IE.

(function (original) {
    console.enableLogging = function () {
        console.log = original;
    console.disableLogging = function () {
        console.log = function () {};
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter