A quick one on a PM2 logging management issue I fell into with large files.

By default, PM2 can create logs. Now if you are working with a production app which goes live and you haven’t sorted out log rotation then there could be a big problem

PM2 for Node is great, read more it here: PM2 for Node

I left the default logging on and on a production app there was an error, it took up about 10 lines for each one. After several days there was a problem with disk space on the server. It was a 40GB server, and there was a PM2 log file that was over 30GB in size!

Now, this was a problem I never had to deal with these logs before.

The server was full and I went into a “delete” it mode by simply using:

Now, this didn’t work because the PM2 process and node were still holding onto that file. So you would have to reboot the server or restart each process using it (was over 15).

So after a bit more reading, I found the PM2 documentation for logging.

Now, this cleared the files in ~/.pm2/logs and the ~/.pm2/pm2.log file that quickly caught up in size after the initial delete.

If you want you can reboot after flushing pm2 logs.
This method fixed the issue of the file still taking up space because it was used by all those processes.
At this stage, I learned about a simple concept of “log rotation”.

PM2 has a module that makes this as simple as a couple of commands to do:

After this, you can set what you want to happen to those log files.
This work for me for my needs:

If you mess up, the default PM2 settings (as of 2020/01/23) are:

This should separate the log files, limit their size and delete them when needed.

Let me know in the comments if this helped you out!

Founder at https://codehubble.com. https://danb.pro | Web and System Engineer. Accessibility. Gaming. Exotic pets.