node.js 将 pm2 登录到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36075460/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:03:49  来源:igfitidea点击:

Make pm2 log to console

node.jspm2

提问by Fragilerus

I am running a node webserver using pm2. Since pm2 spawns another process and redirects stdout and stderr to files, I have to look somewhere else for the logs. Ideally, I would like to have the node process output to the same console window that I've run pm2 from. Otherwise, I would settle for pm2 run the node process with an active console window and have stdout and stderr of the node process write to that console window. How can this be achieved? I'm on a windows machine.

我正在使用 pm2 运行节点网络服务器。由于 pm2 产生另一个进程并将 stdout 和 stderr 重定向到文件,因此我必须在其他地方查找日志。理想情况下,我希望节点进程输出到我运行 pm2 的同一个控制台窗口。否则,我会满足于 pm2 使用活动控制台窗口运行节点进程,并将节点进程的 stdout 和 stderr 写入该控制台窗口。如何做到这一点?我在 Windows 机器上。

回答by Timothy Vann

I believe you can also see the stdoutand stderrof a process that is running daemonized by the command pm2 logsor pm2 logs [app-name].

我相信你也可以看到标准输出标准错误运行该命令一个进程化进程pm2 logspm2 logs [app-name]

回答by Fragilerus

Found the answer (their documentation is not that great), just added the --no-daemonflag, seems to have done it. Although, it appears that it's still logging to the file (even when using the flag) on the first uptime. Once the process gets restarted (I'm watching for file changes) it starts logging out to the console

找到了答案(他们的文档不是很好),刚刚添加了--no-daemon标志,似乎已经完成了。虽然,它似乎仍在第一次正常运行时记录到文件(即使使用标志)。一旦进程重新启动(我正在观察文件更改),它就会开始注销到控制台

回答by kharandziuk

programmatically you can do something like this:

以编程方式,您可以执行以下操作:

const pm2 = require('pm2')

pm2.connect(function(err) {
  if (err) {
    console.error(err);
    process.exit(2);
  }
  pm2.start([
    {
      script             : "server.js",
      output: "/dev/stdout",
      error: "/dev/stderr",
    },
  ]
    , function(err, proc) {
      if(err) {
        throw err
      }
    });
})

回答by Pedro JR

you can easily achieve that by starting another terminal/console and run this command

您可以通过启动另一个终端/控制台并运行此命令来轻松实现

 pm2 log
 // logs everything to the terminal except console.log

 pm2 logs
 // logs everything to the terminal even console.log