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

后端 未结 28 2614
深忆病人
深忆病人 2020-11-22 16:48

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

28条回答
  •  青春惊慌失措
    2020-11-22 17:26

    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);
        }
    }
    

提交回复
热议问题