简体   繁体   English

如何从节点模块中删除/禁用 console.log()

[英]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.

相关问题 如何从ionic的console.log中删除日志? - How do I remove logs from console.log in ionic? 从节点获取console.log结果以在Web控制台中执行 - Get console.log results from node to execute in web console 删除 console.log 中的“空格”? - Remove "the space" in the console.log? 如何根据来自特定 javascript 源(方法、文件)或消息内容的条件禁用 console.log 消息 - How to disable console.log messages based on criteria from specific javascript source (method, file) or message contents 如何在生产环境中禁用 console.log() 并显示横幅? - How to disable console.log() on production and display a banner? 从bash脚本运行节点:输出console.log - Running node from a bash script: output console.log 如何从缩小或合并的 javascript 文件中删除所有 console.log 语句 - How to remove all console.log statements from your minified or combined javascript file 如何使用MVC.NET Bundle配置文件从js文件中删除console.log - How to remove console.log from js files using MVC.NET Bundle config file 如何在发布版本中删除 console.log 行? - How to remove console.log lines on release build? 如何在node.js中使用stack trace记录错误? - How to console.log an error with stack trace in node.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM