是否有任何方法关闭我的JavaScript代码中的所有console.log语句,用于测试目的?


当前回答

警告:无耻的插头!

您还可以使用类似我的JsTrace对象的东西来进行模块化跟踪,并具有模块级别的“切换”功能,从而只打开您当时想要看到的内容。

http://jstrace.codeplex.com

(也有一个NuGet包,为那些谁关心)

所有级别默认为“error”,尽管你可以关闭它们。 不过,我想不出为什么您不希望看到错误

你可以这样改变它们:

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

要获得更多文档,请查看文档

T

其他回答

我这样写道:

//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

    }
}

不幸的是,它在使用严格模式下不起作用。

使用console。fn。setEnabled = setEnabled然后是console。fn。setEnabled(false) fn可以是几乎任何控制台函数。 你的情况是:

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

我还写了这个:

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

所以你只需要在FLAGS中加入默认值(在执行上面的代码之前),比如FLAGS. log = false,日志功能将在默认情况下被禁用,仍然可以调用console.log.setEnabled(true)来启用它

你可以使用logeek,它可以让你控制你的日志消息的可见性。你可以这样做:

<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

你也可以使用logeek.show('nothing')来完全禁用每条日志消息。

我之前用过温斯顿记录器。

现在,我使用以下简单的代码从经验:

从cmd/命令行设置环境变量(在Windows上): cmd setx LOG_LEVEL信息

或者,如果你愿意,你可以在你的代码中有一个变量,但上面更好。

Restart cmd/ command line, or, IDE/ editor like Netbeans Have below like code: console.debug = console.log; // define debug function console.silly = console.log; // define silly function switch (process.env.LOG_LEVEL) { case 'debug': case 'silly': // print everything break; case 'dir': case 'log': console.debug = function () {}; console.silly = function () {}; break; case 'info': console.debug = function () {}; console.silly = function () {}; console.dir = function () {}; console.log = function () {}; break; case 'trace': // similar to error, both may print stack trace/ frames case 'warn': // since warn() function is an alias for error() case 'error': console.debug = function () {}; console.silly = function () {}; console.dir = function () {}; console.log = function () {}; console.info = function () {}; break; } Now use all console.* as below: console.error(' this is a error message '); // will print console.warn(' this is a warn message '); // will print console.trace(' this is a trace message '); // will print console.info(' this is a info message '); // will print, LOG_LEVEL is set to this console.log(' this is a log message '); // will NOT print console.dir(' this is a dir message '); // will NOT print console.silly(' this is a silly message '); // will NOT print console.debug(' this is a debug message '); // will NOT print

现在,根据点1中的LOG_LEVEL设置(例如,setx LOG_LEVEL日志和重新启动命令行),上面的一些将打印,其他将不打印

希望这有帮助。

我认为2020年最简单、最容易理解的方法是创建一个像log()这样的全局函数,你可以选择以下方法之一:

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

你可以说这些功能的缺点是:

您仍然在运行时调用函数 您必须记住在第二个选项中更改调试变量或if语句 您需要确保在加载所有其他文件之前加载了该函数

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?

据我从文档中得知,Firebug没有提供任何变量来切换调试状态。相反,将console.log()包装在一个有条件地调用它的包装器中,即:

DEBUG = true; // set to false to disable debugging
function debug_log() {
    if ( DEBUG ) {
        console.log.apply(this, arguments);
    }
}

为了不需要改变所有现有的调用,你可以使用这个代替:

DEBUG = true; // set to false to disable debugging
old_console_log = console.log;
console.log = function() {
    if ( DEBUG ) {
        old_console_log.apply(this, arguments);
    }
}