Skip to content

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 {
daily
rotate 5
missingok
notifempty
sharedscripts
copytruncate
dateext
}

 

5 Comments

  1. matt wrote:

    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.

    Saturday, April 26, 2014 at 2:36 pm | Permalink
  2. Hey there! I’ve been following your website for a long time now and finally got the courage to go ahead and give you a shout out from Austin Texas! Just wanted to mention keep up the great work!

    Friday, December 5, 2014 at 9:11 am | Permalink
  3. I have learn several just right stuff here. Certainly worth bookmarking for revisiting. I wonder how a lot effort you put to create any such excellent informative site.

    Sunday, December 7, 2014 at 1:32 am | Permalink
  4. TeresitFoss wrote:

    Households that don’t have Wi-Fi enabled satellite boxes can order an On Demand connector to access the service. Identity thieves have gotten quite sophisticated over the years. Please call us at 949-218-8754 for your no-obligation consultation.

    Wednesday, December 10, 2014 at 10:45 pm | Permalink
  5. I am actually thankful to the holder of this website who has shared this enormous article at at this place.

    Wednesday, December 17, 2014 at 4:05 pm | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*