Pm2 changing log file location
Asked Answered
J

5

23

I have couple of questions regarding pm2

  • How can I change the location of server-error-0.log and server-out-0.log files location from c:\users\user\.pm2\logs to other drive, due to restriction in server's c drive access.
  • Can I log the error and info in database instead of a log file? Do I need to write a separate module for that or is there any way to achieve this?
Jamison answered 30/8, 2017 at 6:23 Comment(0)
D
42

How can I change the location of ...log file location?

To change pm2's log file location, there are 2 solutions: define log path as parameter when pm2 command is executed (-l, -o, -e), or start pm2 from a configuration file.

For the parameter solution, here is an example:

pm2 start app.js -o ./out.log -e ./err.log

If you don't want to define log path every time when pm2 is executed, you can generate a configuration file, define error_file and out_file, and start pm2 from that:

  1. Generate a configuration file: pm2 ecosystem simple. This would generate a file ecosystem.config.js, with following content:

    module.exports = {
      apps : [{
        name   : "app1",
        script : "./app.js"
      }]
    }
    
  2. Define error_file (for error log) and out_file (for info log) in the file, such as:

    module.exports = {
      apps : [{
        name   : "app1",
        script : "./app.js",
        error_file : "./err.log",
        out_file : "./out.log"
      }]
    }
    
  3. Delete existing processes in pm2:

    pm2 delete <pid>
    

    You can get pid by doing:

    pm2 status
    
  4. Start the process from the configuration file:

    pm2 start ecosystem.config.js
    

In this way, the logs are saved to ./err.log and ./out.log.

Please refer to the document for detail information.

Can I log the error and info in database instead of a log file?

I didn't find any resources in official document. It seems you need to write code and save log to database yourself.

Deloresdeloria answered 30/8, 2017 at 7:57 Comment(2)
Hi, unfortunately, this does not work with pm2 logrotate for example...Brioche
If you use the parameter solution remember to delete the pid using pm2 delete <pid> as wellFons
R
20

Just wanted to add to @shaochuancs answer, that before doing step 3, make sure you delete the old process. If you don't delete the old process, the changes that you made to your process file will not take into effect after you start your app.

You will need to issue this command before doing step 3 above:

pm2 delete <pid>
Ringside answered 5/10, 2017 at 19:0 Comment(3)
thx man, you just saved me another hour of WTF WTF WTF :) pm2 delete all -> then start againSolatium
I went through a lot of WTFs in last 30 mins, wish I had googled for this before. This details should be included in the accepted answer.Zoophilia
Is there a way to make the change without removing the pid?Logue
L
0

In case you want pm2 on startup with changed logs path:

  1. pm2 delete all
  2. pm2 start ecosystem.js
  3. pm2 save
  4. pm2 startup
Lesser answered 17/6, 2019 at 8:54 Comment(0)
O
0

If you want to write both an error log and console log to the same file, might be a use case, like I am interested to have log in OneFile to push to ELK.you can use -l

-l --log [path]              specify filepath to output both out and error logs

Here is the example

pm2 start server.js -l /app/logs/server.log

After doing changes do not forget to run this command as mentioned in the answer.

pm2 delete <pid>
Overripe answered 24/7, 2019 at 10:47 Comment(0)
E
0

A solution to change the global logs location is to set PM2_HOME, but unfortunately this affects the whole PM2 folder, not only the logs.

Expiration answered 21/7, 2023 at 13:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.