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

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

    My comprehensive solution to disable/override all console.* functions is here.

    Of course, please make sure you are including it after checking necessary context. For example, only including in production release, it's not bombing any other crucial components etc.

    Quoting it here:

    "use strict";
    (() => {
      var console = (window.console = window.console || {});
      [
        "assert", "clear", "count", "debug", "dir", "dirxml",
        "error", "exception", "group", "groupCollapsed", "groupEnd",
        "info", "log", "markTimeline", "profile", "profileEnd", "table",
        "time", "timeEnd", "timeStamp", "trace", "warn"
      ].forEach(method => {
        console[method] = () => {};
      });
      console.log("This message shouldn't be visible in console log");
    })();

提交回复
热议问题