是否有任何方法关闭我的JavaScript代码中的所有console.log语句,用于测试目的?
当前回答
这就是我的方法。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添加了一个保护,而不是猴子补丁。它是安全的。
并且你可以安全地移除所有的控制台。使用该插件登录生产,或者使用与上面相同的配置保留您想要的插件。
其他回答
只需更改标志DEBUG以覆盖console.log函数。这应该能奏效。
var DEBUG = false;
// ENABLE/DISABLE Console Logs
if(!DEBUG){
console.log = function() {}
}
https://stackoverflow.com/a/46189791/871166的简化
switch (process.env.LOG_LEVEL) {
case 'ERROR':
console.warn = function() {};
case 'WARN':
console.info = function() {};
case 'INFO':
console.log = function() {};
case 'LOG':
console.debug = function() {};
console.dir = function() {};
}
我写了一个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..
禁用console.log:
console.log = function() {};
禁用所有写入控制台的功能。
for (let func in console) {
console[func] = function() {};
}
我这样写道:
//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)来启用它
推荐文章
- 如何通过参数和重定向stdin从一个文件到程序运行在gdb?
- 右击是Javascript事件吗?
- 如何找到JavaScript数组中包含的最大数字?
- 在数组中获取所有选中的复选框
- 如何为Firebase构建云函数,以便从多个文件部署多个函数?
- 如何发送推送通知到web浏览器?
- AngularJS:工厂和服务?
- js:将一个组件包装成另一个组件
- 父ng-repeat从子ng-repeat的访问索引
- JSHint和jQuery: '$'没有定义
- 模仿JavaScript中的集合?
- 用JavaScript验证电话号码
- 如何在HTML5中改变视频的播放速度?
- 谷歌地图API v3:我可以setZoom后fitBounds?
- ES6/2015中的null安全属性访问(和条件赋值)