[英]How to remove / disable console.log() from a node module
I am pushing code to production that uses a package that calls console.log() every time I call a specific one of its functions.我正在将代码推送到使用 package 的生产中,每次我调用它的特定功能时都会调用 console.log()。
This function gets called thousands of times simultaneously in my code and I do not want the logging to slow down the execution time or clutter the logs.这个 function 在我的代码中同时被调用了数千次,我不希望日志记录减慢执行时间或使日志混乱。
On local I have gone into the node module and commented the log out, but production is auto deployed and just runs npm install before running.在本地,我已经进入节点模块并注释了注销,但生产是自动部署的,并且在运行前只运行 npm install 。
Is there a way for me to either comment out the line in production in a way that it will not come back every time it auto deploys or a way for me to ban console logs from the module?有没有办法让我在生产中注释掉生产线,使其每次自动部署时都不会回来,或者让我禁止模块中的控制台日志?
console.log
itself can be overwritten with an empty function. console.log
本身可以用空的 function 覆盖。
// backup just in case you need it later somewhere
var oldConsoleLog = window.console.log;
window.console.log = () => {};
However, it's recommended to do some investigation on the troublesome package, most packages log only in their development build, maybe there is a production version of the package you should use.但是,建议对麻烦的 package 进行一些调查,大多数软件包仅在其开发版本中记录,也许您应该使用 package 的生产版本。
Also, there is some babel/webpack plugin that can remove the console statements for you.此外,还有一些 babel/webpack 插件可以为您删除控制台语句。 For example babel-plugin-transform-remove-console
例如 babel-plugin-transform-remove-console
You can try redefining the console.log function by writing console.log = function() {}
at the beginning.您可以尝试通过在开头编写
console.log = function() {}
来重新定义 console.log function。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.