javascript - 如何快速方便地禁用代码中的所有 console.log 语句?

有没有方法可以关闭JavaScript代码中的所有console.log语句,以便进行测试?

时间:

重新定义脚本中的console.log函数。


console.log = function() {}

就是这样,控制台消息被接管。

工作演示→

从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!');
 }
 );

如何使用上面的“logger”?在ready事件中,调用logger.disablelogger,这样控制台消息就不会被记录,在要将消息记录到控制台的方法内,将调用添加到logger.enableLogger和logger.disableLogger。

以下内容更全面:


var DEBUG = false;
if(!DEBUG){
 if(!window.console) window.console = {};
 var methods = ["log","debug","warn","info"];
 for(var i=0;i<methods.length;i++){
 	console[methods[i]] = function(){};
 }
}

如果存在的话,这将使控制台中的常用方法归零,并且可以毫无错误地调用它们,并且几乎没有性能开销。如果没有控制台的话,就会创建一个虚拟方法来防止错误,


if(window.console && !console.dir){
var methods = ["dir","dirxml","trace","profile"]; //etc etc
 for(var i=0;i<methods.length;i++){
 	console[methods[i]] = function(){};
 }
}

从文档可以看出,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);
 }
}


if(typeof(console) === 'undefined') {
 var console = {};
 console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}

如果你使用的是ie7,控制台将不会被定义,因此,更友好的版本是:


if (typeof console =="undefined" || typeof console.log =="undefined") 
{
 var console = { log: function() {} }; 
}

例子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');
});


(function (original) {
 console.enableLogging = function () {
 console.log = original;
 };
 console.disableLogging = function () {
 console.log = function () {};
 };
})(console.log);

你可以像这样更改它们:


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

更多文档请查看文档

我在这个url中发现了一个更高级的代码JavaScript技巧:禁用和禁用console.log


var DEBUG_MODE = true; // Set this value to false for production

if(typeof(console) === 'undefined') {
 console = {}
}

if(!DEBUG_MODE || typeof(console.log) === 'undefined') {
 // FYI: Firebug might get cranky...
 console.log = console.error = console.info = console.debug = console.warn = console.trace = console.dir = console.dirxml = console.group = console.groupEnd = console.time = console.timeEnd = console.assert = console.profile = function() {};
}

只需更改标记DEBUG以覆盖console.log函数,这玩意很有用。


var DEBUG = false;
// ENABLE/DISABLE Console Logs
if(!DEBUG){
 console.log = function() {}
}

...