I will tell you guys how to setup pm2 and pm2-logrotate. For this setup, I’m gonna use MacOS to install but pm2 is as part of the npm, there will be the same command for other Operation System like Windows and Linux. Furthermore, PM2 is a daemon process manager that will help your application to keep online and auto-start even though server reboot.
Firstly, install the pm2 latest version (global) using npm command in terminal.
npm install pm2@latest -g i
And sometimes, you may receive the installation failed because there have Missing write access to /usr/local/lib/node_modules. If you faced that issue, please change the user to root and retry again then it should be fine already.
Secondly, start the node application server and point the app log destination using this command.
pm2 start server.js -o ./logs/trace.log
Thirdly, install the pm-logrotate using below npm command.
pm2 install pm2-logrotate
Finally, I will execute following commands to configure the Max log rotate size to 100MB, compress the logs when rotated and rotate every day at midnight.
pm2 set pm2-logrotate:max_size 100M
pm2 set pm2-logrotate:compress true
pm2 set pm2-logrotate:rotateInterval '0 0 * * *'