Rotate and Compress pm2 logs Elegantly

pm2 logs can grow big overtime. As big as 50GB or more in a month time based on your application usage. Eventually you may run out of disk space. Here are the simple steps to manage it.
features. This should take care of auto rotating, compressing and managing logs.
sudo pm2 install pm2-logrotate
If you get an error saying pm2: command not found, then install pm2 using the command sudo npm i -g pm2

Additional step: If you want to make sure the pm2-logrotate makes use of native logrorate, then run the command
sudo pm2 logrotate -u your-server-username .

To make sure the native logrotate is working fine, run the command:
logrotate -f /etc/logrotate.d/pm2-your-server-username
While this will show empty output and return to command prompt, you will be able to see certain log files compressed (.gz files) at the usual logs folder location of pm2, that is at ~/.pm2/logs

Comments (2)

I tried to install **pm2-logrotate** got

deploy@wgn-staging:~$ sudo pm2 install pm2-logrotate
sudo: pm2: command not found

then ran the below command
sudo npm i -g pm2

After that **sudo pm2 install pm2-logrotate** ran successfully. Can you please add these steps also in your blog.

Thanks Vinothini.
Added those steps.

Leave a comment