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

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

    I think that the easiest and most understandable method in 2020 is to just create a global function like log() and you can pick one of the following methods:

    const debugging = true;
    
    function log(toLog) {
      if (debugging) {
        console.log(toLog);
      }
    }
    
    function log(toLog) {
      if (true) { // You could manually change it (Annoying, though)
        console.log(toLog);
      }
    }
    

    You could say that the downside of these features is that:

    1. You are still calling the functions on runtime
    2. You have to remember to change the debugging variable or the if statement in the second option
    3. You need to make sure you have the function loaded before all of your other files

    And my retorts to these statements is that this is the only method that won't completely remove the console or console.log function which I think is bad programming because other developers who are working on the website would have to realize that you ignorantly removed them. Also, you can't edit JavaScript source code in JavaScript, so if you really want something to just wipe all of those from the code you could use a minifier that minifies your code and removes all console.logs. Now, the choice is yours, what will you do?

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

    If you use Webpack you could use the Terser plugin to completely exclude the console.log function calls.

    This way you can have a clean production app package that will not expose unnecessary information but still have all this info in your debug build.

    https://github.com/terser/terser#compress-options

    drop_console (default: false) -- Pass true to discard calls to console.* functions. If you wish to drop a specific function call such as console.info and/or retain side effects from function arguments after dropping the function call then use pure_funcs instead.

    minimizer: [
        new TerserPlugin({
            terserOptions: {
                compress: {
                    pure_funcs: [ 'console.log' ]
                }
            }
        }),
    ]
    

    Alternatively you can use drop_console: true to exclude all console calls.

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

    You can use logeek, It allows you to control your log messages visibility. Here is how you do that:

    <script src="bower_components/dist/logeek.js"></script>
    
    logeek.show('security');
    
    logeek('some message').at('copy');       //this won't be logged
    logeek('other message').at('secturity'); //this would be logged
    

    You can also logeek.show('nothing') to totally disable every log message.

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

    I wrote this:

    //Make a copy of the old console.
    var oldConsole = Object.assign({}, console);
    
    //This function redefine the caller with the original one. (well, at least i expect this to work in chrome, not tested in others)
    function setEnabled(bool) {
        if (bool) {
            //Rewrites the disable function with the original one.
            console[this.name] = oldConsole[this.name];
            //Make sure the setEnable will be callable from original one.
            console[this.name].setEnabled = setEnabled;
        } else {
            //Rewrites the original.
            var fn = function () {/*function disabled, to enable call console.fn.setEnabled(true)*/};
            //Defines the name, to remember.
            Object.defineProperty(fn, "name", {value: this.name});
            //replace the original with the empty one.
            console[this.name] = fn;
            //set the enable function
            console[this.name].setEnabled = setEnabled
    
        }
    }
    

    Unfortunately it doesn't work on use strict mode.

    So using console.fn.setEnabled = setEnabled and then console.fn.setEnabled(false) where fn could be almost any console function. For your case would be:

    console.log.setEnabled = setEnabled;
    console.log.setEnabled(false);
    

    I wrote this too:

    var FLAGS = {};
        FLAGS.DEBUG = true;
        FLAGS.INFO = false;
        FLAGS.LOG = false;
        //Adding dir, table, or other would put the setEnabled on the respective console functions.
    
    function makeThemSwitchable(opt) {
        var keysArr = Object.keys(opt);
        //its better use this type of for.
        for (var x = 0; x < keysArr.length; x++) {
            var key = keysArr[x];
            var lowerKey = key.toLowerCase();
            //Only if the key exists
            if (console[lowerKey]) {
                //define the function
                console[lowerKey].setEnabled = setEnabled;
                //Make it enabled/disabled by key.
                console[lowerKey].setEnabled(opt[key]);
            }
        }
    }
    //Put the set enabled function on the original console using the defined flags and set them.
    makeThemSwitchable(FLAGS);
    

    so then you just need to put in the FLAGS the default value (before execute the above code), like FLAGS.LOG = false and the log function would be disabled by default, and still you could enabled it calling console.log.setEnabled(true)

    0 讨论(0)
提交回复
热议问题