是否有任何方法关闭我的JavaScript代码中的所有console.log语句,用于测试目的?
当前回答
这应该覆盖window.console的所有方法。你可以把它放在你的脚本部分的最上面,如果你在一个PHP框架上,你只能在你的应用程序环境是生产的时候打印这段代码,或者当某种调试标志被禁用的时候。然后,代码中的所有日志都将在开发环境或调试模式下工作。
window.console = (function(originalConsole){
var api = {};
var props = Object.keys(originalConsole);
for (var i=0; i<props.length; i++) {
api[props[i]] = function(){};
}
return api;
})(window.console);
其他回答
在脚本中重新定义console.log函数。
console.log = function() {}
够了,不再给控制台发消息了。
编辑:
扩展了Cide的想法。一个自定义记录器,您可以使用它从代码中切换登录。
从我的Firefox控制台:
var logger = function()
{
var oldConsoleLog = null;
var pub = {};
pub.enableLogger = function enableLogger()
{
if(oldConsoleLog == null)
return;
window['console']['log'] = oldConsoleLog;
};
pub.disableLogger = function disableLogger()
{
oldConsoleLog = console.log;
window['console']['log'] = function() {};
};
return pub;
}();
$(document).ready(
function()
{
console.log('hello');
logger.disableLogger();
console.log('hi', 'hiya');
console.log('this wont show up in console');
logger.enableLogger();
console.log('This will show up!');
}
);
如何使用上面的“记录器”?在就绪事件中,调用记录器。disableLogger使控制台消息不被记录。向记录器添加调用。enabllogger和logger。在希望将消息记录到控制台的方法中的disableLogger。
在我搜索了这个问题以及在我的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) {
}
这就是我的方法。https://github.com/jchnxu/guard-with-debug 免责声明:我是这个小实用程序的作者
localStorage.debug = [
'enable/console/log/in/this/file.ts',
'enable/console/log/in/this/folder/*',
'-disable/console/log/in/this/file.ts',
'-disable/console/log/in/this/folder/*',
// enable all
'*',
].join(',');
这种方法的好处是我们可以基于文件名启用/禁用,在以下情况下尤其有用
跨多个复杂模块调试 输出内容很大,在视觉上令人不安 通过触摸代码库快速打开/关闭日志记录,当您只是部署到有更多日志的生产环境时,这会更安全。
为什么要触摸console.log?危险吗?
这多少有点自以为是。我发现自己写了很多主机游戏。而不是更标准的方式从'xxx' && logger.info(…)导入记录器。我想原因是console.log是js原生的,它感觉更自然。
这个插件只是给console.log添加了一个保护,而不是猴子补丁。它是安全的。
并且你可以安全地移除所有的控制台。使用该插件登录生产,或者使用与上面相同的配置保留您想要的插件。
你不应该!
重写内置函数不是一个好的做法。也不能保证你会抑制所有输出,你使用的其他库可能会与你的更改冲突,还有其他函数可能会写入控制台;.warning .dir (), (), . error (), .debug (), . assert()等。
正如一些人建议的那样,您可以定义一个DEBUG_MODE变量并有条件地记录日志。根据代码的复杂性和性质,编写自己的记录器对象/函数可能是一个好主意,它包装了控制台对象并内置了此功能。那将是处理仪器的正确地方。
也就是说,出于“测试”的目的,您可以编写测试,而不是打印到控制台。如果您不做任何测试,而那些console.log()行只是编写代码的辅助,那么只需删除它们。
我写了一个ES2015解决方案(仅用于Webpack)。
class logger {
static isEnabled = true;
static enable () {
if(this.constructor.isEnabled === true){ return; }
this.constructor.isEnabled = true;
}
static disable () {
if(this.constructor.isEnabled === false){ return; }
this.constructor.isEnabled = false;
}
static log () {
if(this.constructor.isEnabled === false ) { return; }
const copy = [].slice.call(arguments);
window['console']['log'].apply(this, copy);
}
static warn () {
if(this.constructor.isEnabled === false ) { return; }
const copy = [].slice.call(arguments);
window['console']['warn'].apply(this, copy);
}
static error () {
if(this.constructor.isEnabled === false ) { return; }
const copy = [].slice.call(arguments);
window['console']['error'].apply(this, copy);
}
}
描述:
Along with logger.enable and logger.disable you can use console.['log','warn','error'] methods as well using logger class. By using logger class for displaying, enabling or disabling messages makes the code much cleaner and maintainable. The code below shows you how to use the logger class: logger.disable() - disable all console messages logger.enable() - enable all console messages logger.log('message1', 'message2') - works exactly like console.log. logger.warn('message1', 'message2') - works exactly like console.warn. logger.error('message1', 'message2') - works exactly like console.error. Happy coding..
推荐文章
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)
- 与push()相反;
- JS字符串“+”vs concat方法
- AngularJS使用ng-class切换类
- 访问Handlebars.js每次循环范围之外的变量
- 如何用JavaScript截屏一个div ?
- 如何为其他域设置cookie
- 我如何调试git/git-shell相关的问题?
- 如何减去日期/时间在JavaScript?
- 如何检测“搜索”HTML5输入的清除?