在将 nodejs 应用程序部署到生产环境之前删除所有 console.log?

Remove all console.log before deploying nodejs app to production?

这个问题之前可能有人问过。我即将在安装了 pm2 的情况下将 nodejs 10.16 应用程序部署到生产环境。以下是关于 nodejs 部署不能做什么的 article

Not removing console.log statements

While developing an app, we use console.log statements to test things out. But sometimes we forget to remove these log statements in production, which consume the CPU time and waste the resources. The best way to avoid this is to use debug module. So, unless you start your app with environment variable DEBUG nothing will be printed to the console.

没有关于如果我想在控制台屏幕上接收错误输出以进行监视和调试时该怎么做的更多详细信息。由于我开发中的代码中有很多行 console.log 。在部署到生产环境之前,我是否必须将它们全部删除?生产环境中 console.log 的标准做法是什么?

最好的方法可能是将 console.log 包装在您的应用中,并且仅在环境为 "development" 时显示 "logs"。

另一种方法,如果您赶时间,可以将其添加到 server.js 或类似的顶部(在 require 语句之后):

console.log = function(){};

你可能想看看这个 npm 包 https://www.npmjs.com/package/no-console