覆盖console.log(); 生产

我相当新的JavaScript开发,所以这可能是一个真正的新手问题。

我有一个sencha触摸应用程序与console.log(); 用于debugging目的。

我的整个构build时间都在拼凑 它输出用于debugging的app.debug.js以及用于生产的app.min.js

现在我可以通过我所有的代码文件寻找console.log(); 并在我准备好进行生产时手动删除它,但我想知道是否有方法来重写该方法。

基本上,每当console.log(); 方法被调用,什么也不做。

这样,我可以把覆盖代码文件在我的生产configuration,而不是在我的debuggingconfiguration。

这可能吗?

把它放在文件的顶部:

 var console = {}; console.log = function(){}; 

对于某些浏览器和缩小器,可能需要将其应用到窗口对象上。

 window.console = console; 

能够在生产版本中切换日志将是非常有用的。 下面的代码默认closureslogging器。

当我需要查看日志时,只需在控制台中inputdebug(true)

 var consoleHolder = console; function debug(bool){ if(!bool){ consoleHolder = console; console = {}; Object.keys(consoleHolder).forEach(function(key){ console[key] = function(){}; }) }else{ console = consoleHolder; } } debug(false); 

为了彻底,这覆盖了所有的控制台方法,而不仅仅是console.log

或者如果你只是想重新定义控制台的行为(例如为了添加日志)你可以做这样的事情:

 // define a new console var console=(function(oldCons){ return { log: function(text){ oldCons.log(text); // Your code }, info: function (text) { oldCons.info(text); // Your code }, warn: function (text) { oldCons.warn(text); // Your code }, error: function (text) { oldCons.error(text); // Your code } }; }(window.console)); //Then redefine the old console window.console = console; 
 console.log = function(){}; 

像其他任何东西一样覆盖它。

我使用类似于什么样的实验室的东西。 将控制台保存在闭包中,并将其全部放在一个便携式函数中。

 var GlobalDebug = (function () { var savedConsole = console; return function(debugOn,suppressAll){ var suppress = suppressAll || false; if (debugOn === false) { console = {}; console.log = function () { }; if(suppress) { console.info = function () { }; console.warn = function () { }; console.error = function () { }; } else { console.info = savedConsole.info; console.warn = savedConsole.warn; console.error = savedConsole.error; } } else { console = savedConsole; } } })(); 

只需执行globalDebug(false)即可closures日志消息,或使用globalDebug(false,true)删除所有控制台消息。

我会build议使用: https : //github.com/sunnykgupta/jsLogger

特征:

  1. 它安全地覆盖了console.log。
  2. 注意,如果控制台不可用(哦,是的,你也需要考虑到这一点)。
  3. 存储所有日志(即使它们被禁止)以备以后检索。
  4. 处理主要的控制台function,如logwarnerrorinfo

是开放的修改,并会在新的build议出现时更新。

免责声明:我是该插件的作者。

如果不再需要,你也可以使用regex来删除代码中的所有console.log()调用。 任何体面的IDE都将允许您在整个项目中search并replace这些内容,并允许您在提交更改之前预览匹配项。

 \s*console\.log\([^)]+\); 

只要记住用这个方法每个console.log调用仍然会调用一个(空)函数导致开销,如果有100个console.log命令,你仍然在做一个空函数的100个调用。

不知道这会造成多less开销,但会有一些,最好有一个标志来打开debugging,然后使用的东西沿线:

 var debug=true; if (debug) console.log('blah') 

Theres没有任何理由让所有的console.log在prod环境中遍布你的项目…如果你想以正确的方式进行,使用“drop_console”选项将UglifyJS2添加到部署过程中。

阅读了很多post之后,我做出了自己的解决scheme如下:

脚本:

 function extendConsole() { "use strict"; try { var disabledConsoles = {}; console.enable = function (level, enabled) { // Prevent errors in browsers without console[level] if (window.console === 'undefined' || !window.console || window.console === null) { window.console = {}; } if (window.console[level] === 'undefined' || !window.console[level] || window.console[level] == null) { window.console[level] = function() {}; } if (enabled) { if (disabledConsoles[level]) { window.console[level] = disabledConsoles[level]; } console.info("console." + level + "() was enabled."); } else { disabledConsoles[level] = window.console[level]; window.console[level] = function () { }; console.info("console." + level + "() was disabled."); } }; } catch (exception) { console.error("extendConsole() threw an exception."); console.debug(exception); } } 

用法:

 extendConsole(); console.enable("debug", window.debugMode); 

例:

http://jsfiddle.net/rodolphobrock/2rzxb5bo/10/

你可以看看UglifyJS : http : UglifyJS : //github.com / mishoo / UglifyJS我还没有尝试过。

引用,

  if (typeof DEBUG === 'undefined') DEBUG = true; // will be removed function doSomethingCool() { DEBUG && console.log("something cool just happened"); // will be removed } 

…日志消息行将被Uglify的死代码移除器删除(因为它将删除总是评估为false的任何条件)。 那么,那第一个条件。 但是当你testing的是未压缩的代码时,DEBUG会从未定义的开始,第一个条件会将它设置为true,并且所有的console.log()消息都可以工作。

这是我做的

  var domainNames =["fiddle.jshell.net"]; // we replace this by our production domain. var logger = { force:false, original:null, log:function(obj) { var hostName = window.location.hostname; if(domainNames.indexOf(hostName) > -1) { if(window.myLogger.force === true) { window.myLogger.original.apply(this,arguments); } }else { window.myLogger.original.apply(this,arguments); } }, forceLogging:function(force){ window.myLogger.force = force; }, original:function(){ return window.myLogger.original; }, init:function(){ window.myLogger.original = console.log; console.log = window.myLogger.log; } } window.myLogger = logger; console.log("this should print like normal"); window.myLogger.init(); console.log("this should not print"); window.myLogger.forceLogging(true); console.log("this should print now"); 

也发布在这里。 http://bhavinsurela.com/naive-way-of-overriding-console-log/

当url不包含localhost时,这将覆盖console.log函数。 您可以用您自己的开发设置replace本地主机。

 // overriding console.log in production if(window.location.host.indexOf('localhost:9000') < 0) { console.log = function(){}; }