是否有任何方法关闭我的JavaScript代码中的所有console.log语句,用于测试目的?
当前回答
我的全面解决方案禁用/覆盖所有控制台。*函数在这里。
当然,请确保在检查必要的上下文之后包含它。例如,只包含在产品版本中,它不会轰炸任何其他关键组件等。
这里引用一下:
“使用严格的”; (() => { Var控制台=(窗口。控制台=窗口。控制台|| {}); [ “维护”、“清楚”、“数”、“调试”,“dir”、“dirxml”, "error", "exception", "group", " groupcollapse ", "groupEnd", "info", "log", "markTimeline", "profile", "profileEnd", "table", "time" "timeEnd" "timeStamp" "trace" "warn" ]。forEach(方法=> { Console [method] = () => {}; }); console.log("此消息不应该在控制台日志中可见"); })();
其他回答
一行代码设置devMode为true/false;
console.log = devMode ?console.log:() => {};
我认为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?
这是来自SolutionYogi和Chris s的答案的混合。它维护console.log行号和文件名。jsFiddle示例。
// Avoid global functions via a self calling anonymous one (uses jQuery)
(function(MYAPP, $, undefined) {
// Prevent errors in browsers without console.log
if (!window.console) window.console = {};
if (!window.console.log) window.console.log = function(){};
//Private var
var console_log = console.log;
//Public methods
MYAPP.enableLog = function enableLogger() { console.log = console_log; };
MYAPP.disableLog = function disableLogger() { console.log = function() {}; };
}(window.MYAPP = window.MYAPP || {}, jQuery));
// Example Usage:
$(function() {
MYAPP.disableLog();
console.log('this should not show');
MYAPP.enableLog();
console.log('This will show');
});
在我搜索了这个问题以及在我的cordova应用程序中尝试后,我只是想警告每个windows phone的开发者不要覆盖
console.log
因为应用程序在启动时会崩溃。
如果你幸运的话,它不会崩溃,但在商店中提交它会导致应用程序崩溃。
只是覆盖
window.console.log
如果你需要的话。
这在我的应用程序工作:
try {
if (typeof(window.console) != "undefined") {
window.console = {};
window.console.log = function () {
};
window.console.debug = function () {
};
window.console.info = function () {
};
window.console.warn = function () {
};
window.console.error = function () {
};
}
if (typeof(alert) !== "undefined") {
alert = function ()
{
}
}
} catch (ex) {
}
你不应该!
重写内置函数不是一个好的做法。也不能保证你会抑制所有输出,你使用的其他库可能会与你的更改冲突,还有其他函数可能会写入控制台;.warning .dir (), (), . error (), .debug (), . assert()等。
正如一些人建议的那样,您可以定义一个DEBUG_MODE变量并有条件地记录日志。根据代码的复杂性和性质,编写自己的记录器对象/函数可能是一个好主意,它包装了控制台对象并内置了此功能。那将是处理仪器的正确地方。
也就是说,出于“测试”的目的,您可以编写测试,而不是打印到控制台。如果您不做任何测试,而那些console.log()行只是编写代码的辅助,那么只需删除它们。
推荐文章
- jQuery中的live()转换为on()
- 如何区分鼠标的“点击”和“拖动”
- IE9是否支持console.log,它是一个真实的功能吗?
- Node.js同步执行系统命令
- 如何转义JSON字符串包含换行字符使用JavaScript?
- jQuery等价于JavaScript的addEventListener方法
- jQuery需要避免的陷阱
- JavaScript中变量字符串的XML解析
- 'React'指的是一个UMD全局,但当前文件是一个模块
- 为什么useState不触发重新渲染?
- 如何使用回调与useState挂钩在反应
- 网络请求失败
- 如何使用JavaScript大写字符串中每个单词的第一个字母?
- 如何使用箭头函数(公共类字段)作为类方法?
- 使用Javascript的atob解码base64不能正确解码utf-8字符串