How to Rotate Logs using PM2 Process Manager for Node.js

girl reading service manual

Pm2 is a great utility for keeping your Node production applications running.  It  provides high-level process management, and consolidates all your logs into one common location.  In addition, you can view those logs in a real-time manner using the PM2 logs command.

I now use PM2 in all new Node installations, heavily depending on its ability to capture all Node output into easily organized logs.  One feature lacking is the built-in ability to rotate your log files.  This can create some pretty large files if you are not careful.  Because PM2 does not accept a signal to rotate the log, you need to use the copytruncate capability of logrotate.   Simply add the following in a file called PM2 in /etc/logrotate.d. You can change the frequency (daily, weekly, etc),  number of copies to keep (rotate 5), and you can use the compress option if you want the logs to be compressed when rotated.  I usually skip this step, I prefer to easily be able to access the files without having to uncompress first.

Let me know if you have any other handy tips for using PM2.

/root/.pm2/logs/*.log {
rotate 5


One thought on “How to Rotate Logs using PM2 Process Manager for Node.js

  1. matt

    I’m using winston for logging instead. It handles log rotation and can do console/file logging with multiple configs for different ‘loggers’. I think at a certain point, it makes sense to move to something like this instead of relying on console.log.

Leave a Reply and Let me know what you think...