日志管理

启用日志pm2.log的时间戳前缀

export PM2_LOG_DATE_FORMAT="YYYY-MM-DD HH:mm Z"

If this env-variable has been changed, you need to dump your processes and kill daemon, restart it again to take effect, e.g.:

$ pm2 dump
$ pm2 [resurrect|save]

实时查看日志

Monit

Displaying logs of specified process or all processes in realtime:

$ pm2 logs
$ pm2 logs big-api
$ pm2 flush # Clear all the logs

高级日志接口

Navigate between processes logs in realtime with an ergonomic interface:

$ pm2 ilogs

Reloading all logs (SIGUSR2/Logrotate)

To reload all logs, you can send SIGUSR2 to the PM2 process.

You can also reload all logs via the command line with:

$ pm2 reloadLogs

Options

--merge-logs : merge logs from different instances but keep error and out separated
--log-date-format <format>: prefix logs with formated timestamp (http://momentjs.com/docs/#/parsing/string-format/)

Merge out and err logs

  • If you only want to merge out and err logs into one output file, try with the following examples:

    $ pm2 start name.js -o name.log -e name.log
    
    {
      "apps" : [{
        "name"        : "name",
        "script"      : "name.js",
        "err_file"    : "name.log",
        "out_file"    : "name.log"
      }]
    }
    
  • How about merge out and err logs into one, and also keep the separated logs? e.g.:

    $ pm2 start -l
    
    $ pm2 start -l name.log
    
    {
      "apps" : [{
        "name"        : "name",
        "script"      : "name.js",
        "log_file"    : true
      }]
    }
    
    {
      "apps" : [{
        "name"        : "name",
        "script"      : "name.js",
        "log_file"    : "name.log"
      }]
    }
    

    Notes: When you providing a Boolean (true) value for -l, --log option or log_file property, it means a merged log file will be automatic generated, i.e. ~/.pm2/logs/[name]-[id].log, otherwise it's specific.