Make pm2 log to console

Viewed 97237

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.

5 Answers

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 and console.log

notice the 's' in the second command

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
      }
    });
})

Direct to the app running that you want to monit logs

pm2 logs <app id or app name> --lines 50
Related