Pm2: How can I combine the output and error logs?

Created on 7 Mar 2014  路  8Comments  路  Source: Unitech/pm2

forever has a -l option that directs both STDOUT and STDERR of the app into the same file. This is useful as I can see after which operations (output via console.log) some error has occurred (output via console.error).

How can I do this with pm2?

Most helpful comment

Could we maybe reopen this issue? As I see, --merge-logs combines logs from instanced, but not STDOUT and STDERR.

Here's a workaround I found, until pm2 enables support for this. It's a bit more complicated because of the numbers added to the end of the log filenames.

  MYLOG_BASENAME="myapp.log"
  pm2 start myapp.js -o $MYLOG_BASENAME.out -e $MYLOG_BASENAME.err
  ACTUAL_LOG=$(pm2 -m l | grep -o /.\*$MYLOG_BASENAME.\*out)
  ACTUAL_ERR=$(pm2 -m l | grep -o /.\*$MYLOG_BASENAME.\*err)
  tail -f $ACTUAL_LOG $ACTUAL_ERR | tee -a $MYLOG_BASENAME.both

Please let me know if there's a simpler way to achieve this.

All 8 comments

I don't think it's a good idea but perhaps that setting the same output file into a JSON declaration would work ;).

I tried passing the same output file to -o and -e, but pm2 logs was confused by that. Every STDOUT line was shown both as "out" and as "err".

You can now merge all logs of the same process name to the same output and error file via the option --merge-logs

Let's say I only have one process. Can --merge-logs interleave the -e (error) and -o (output) logs into one file? This would assist in seeing what has happened before an error occurred, because the error message itself in a separate file will be out of context.

Could we maybe reopen this issue? As I see, --merge-logs combines logs from instanced, but not STDOUT and STDERR.

Here's a workaround I found, until pm2 enables support for this. It's a bit more complicated because of the numbers added to the end of the log filenames.

  MYLOG_BASENAME="myapp.log"
  pm2 start myapp.js -o $MYLOG_BASENAME.out -e $MYLOG_BASENAME.err
  ACTUAL_LOG=$(pm2 -m l | grep -o /.\*$MYLOG_BASENAME.\*out)
  ACTUAL_ERR=$(pm2 -m l | grep -o /.\*$MYLOG_BASENAME.\*err)
  tail -f $ACTUAL_LOG $ACTUAL_ERR | tee -a $MYLOG_BASENAME.both

Please let me know if there's a simpler way to achieve this.

Any updates? The workaround above require running tail all the time, in parallel with the app. Cumbersome.

Seems no good options here. My best bet is to use Heka or Logstash listening on TCP to manage logs yourself.

1996

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jubairsaidi picture jubairsaidi  路  3Comments

morfies picture morfies  路  3Comments

webchaz picture webchaz  路  3Comments

ldarren picture ldarren  路  3Comments

rangercyh picture rangercyh  路  4Comments