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

后端 未结 28 2218
深忆病人
深忆病人 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:31

    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 () {};
        };
    })(console.log);
    
    0 讨论(0)
  • 2020-11-22 17:31

    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 讨论(0)
  • 2020-11-22 17:32

    You should not!

    It is not a good practice to overwrite built-in functions. There is also no guarantee that you will suppress all output, other libraries you use may revert your changes and there are other functions that may write to the console; .dir(), .warning(), .error(), .debug(), .assert() etc.

    As some suggested, you could define a DEBUG_MODE variable and log conditionally. Depending on the complexity and nature of your code, it may be a good idea to write your own logger object/function that wraps around the console object and has this capability built-in. That would be the right place to do deal with instrumentation.

    That said, for 'testing' purposes you can write tests instead of printing to the console. If you are not doing any testing, and those console.log() lines were just an aid to write your code, simply delete them.

    0 讨论(0)
  • 2020-11-22 17:34

    A simplification of https://stackoverflow.com/a/46189791/871166

    switch (process.env.LOG_LEVEL) {
      case 'ERROR':
        console.warn = function() {};
      case 'WARN':
        console.info = function() {};
      case 'INFO':
        console.log = function() {};
      case 'LOG':
        console.debug = function() {};
        console.dir = function() {};
    }
    
    
    0 讨论(0)
  • 2020-11-22 17:34

    After doing some research and development for this problem, I came across this solution which will hide warnings/Errors/Logs as per your choice.

        (function () {
        var origOpen = XMLHttpRequest.prototype.open;
        XMLHttpRequest.prototype.open = function () {        
            console.warn = function () { };
            window['console']['warn'] = function () { };
            this.addEventListener('load', function () {                        
                console.warn('Something bad happened.');
                window['console']['warn'] = function () { };
            });        
        };
    })();
    

    Add this code before JQuery plugin (e.g /../jquery.min.js) even as this is JavaScript code that does not require JQuery. Because some warnings are in JQuery itself.

    Thanks!!

    0 讨论(0)
  • 2020-11-22 17:35

    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.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time =    console.timeEnd = console.assert = console.profile = function() {};
    }
    
    0 讨论(0)
提交回复
热议问题