Search code examples
node.jsloggingpm2

Write PM2 logs to custom path


I use PM2 to execute my Node.js app.
In order to do that, I have defined the following ecosystem config file:

apps:
  - script: app.js
    name: "myApp"
    exec_mode: cluster
    cwd: "/etc/myService/myApp"

Everything is working. Now I want to specify the custom location for the PM2's logs, therefore I added into ecosystem config file:

log: "/etc/myService/myApp/logs/myApp.log"

It works, but I paid attention that after execution of pm2 start ecosystem, PM2 will write the logs to both locations at the same time:

  • /etc/myService/myApp/logs/myApp.log (as expected)
  • /home/%$user%/.pm2/logs/ (default logs destination)

How can I specify the only place for logs of PM2 and avoid the duplicate logs generation?


Solution

  • Based on the comment of robertklep, in order to solve the issue we have to use out_file and err_file fields for output and error log paths respectively.

    Syntax sample in YAML format:

    out_file: "/etc/myService/myApp/logs/myApp_L.log"
    err_file: "/etc/myService/myApp/logs/myApp_E.log"
    

    P.S. The field log can be removed from the config file:

    log: "/etc/myService/myApp/logs/myApp.log"