如何使用 DEBUG 选项启动 pm2 进程

How to start pm2 process with DEBUG option

我有一个 express 应用程序,我在终端中使用以下命令在其中启用调试日志:

DEBUG=custom:* npm start (on Ubuntu)
SET DEBUG=custom:* & npm start (on Windows)

在生产服务器上,我使用以下命令使用 PM2 启动应用程序:

pm2 start bin/www -i 0

但这不会在我的代码中启用 debug 日志,因此调试语句不会添加到日志中,只有 console.error() 会添加到日志文件中。如何在使用 PM2 启动我的应用程序时传递 DEBUG=custom:* 选项?

尝试DEBUG='custom:*' pm2 start bin/www -i 0

如果您要重新启动现有进程,请添加 --update-env 标志:

DEBUG='custom:*' pm2 restart bin/www -i 0 --update-env

Mikko 是正确的,但如果您将其添加到 package.json 脚本中,它将无法工作!

"scripts": {
    "start": "DEBUG='custom:*' pm2 start bin/www -i 0",
    ...
  },

因为 DEBUG='custom:*' 给了 pm2 进程,而不是你的进程。所以在这种情况下,您必须使用生态系统文件并在生态系统文件中添加 DEBUG 设置,例如

"scripts": {
    "start": "pm2 start ecosystem.config.js",
    ...
  },

//in ecosystem.config.js add this
env: {
    NODE_ENV: 'development',
    DEBUG: 'custom:*'
},