In this article, we will see the PM2 logrotate config and How to “Install PM2 logrotate with the PM2 command”.
The PM2(Process Manager) is also one of the NPM packages. And many of the contributors worked to make PM2 one of the best Managing tools.
PM2 logrotate automatic rotate the logs, delete the logs, and compresses the logs. All we have to do is the configuration of the PM2 logrotate.
How to Install PM2 logrotate via PM2 command.
Let’s go to a bash and install PM2 logrotate,
$ sudo pm2 install pm2-logrotate
There is a worker Interval that runs every 30 sec into the pm2-logrotate and executes the configurations.
So, the PM2 logrotate config has some of the default values, so let’s discuss them in detail.
- max_size: By default, it is 10 MB, If the log file size is 10 MB, then it will run the pm2-logrotate and will make a new log file.
- retain: By default value is 30, so the logs older than 30 files will get deleted.
- compress: By default value is false, set the value to true want compression.
- dateFormat: YYYY-MM-DD_HH-mm-ss this is by default date format.
- rotateInterval: By default will set 0 0 * * * at everyday in midnight.
So, we can change the rotateInterval it’s the same as a crontab in Linux. Want more on the pm2-logrotate official.
How to check the default configuration for PM2-logrotate.
Let’s suppose you have to check the default value of the configuration, then use “pm2 config”.
$ sudo pm2 config pm2-logrotate
[email protected]:~$ sudo pm2 config pm2-logrotate Module: pm2-logrotate $ pm2 set pm2-logrotate:max_size 10M $ pm2 set pm2-logrotate:retain 30 $ pm2 set pm2-logrotate:compress false $ pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss $ pm2 set pm2-logrotate:workerInterval 30 $ pm2 set pm2-logrotate:rotateInterval 0 0 * * * $ pm2 set pm2-logrotate:rotateModule true Modules configuration. Copy/Paste line to edit values. [email protected]:~$
How to compress the logs.
So to compress the logs, by default value is false. Set it to true to compress the logs. It will compress the logs when the logs get rotated.
$ sudo pm2 set pm2-logrotate:compress true
[email protected]:~$ sudo pm2 set pm2-logrotate:compress true [PM2] Module pm2-logrotate restarted [PM2] Setting changed Module: pm2-logrotate $ pm2 set pm2-logrotate:max_size 10M $ pm2 set pm2-logrotate:retain 30 $ pm2 set pm2-logrotate:compress true $ pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss $ pm2 set pm2-logrotate:workerInterval 30 $ pm2 set pm2-logrotate:rotateInterval 0 0 * * * $ pm2 set pm2-logrotate:rotateModule true [email protected]:~$
How to set the size for the log rotation.
So, now we will change the max_size of the log file by default it is 10 MB, we will set the new max_size to 20 MB.
Once the log file size reaches 20 MB, it will get rotated, and the new 0 bytes will appear.
$ sudo pm2 set pm2-logrotate:size 20 M
How to delete old logs in PM2.
So by default, the log file we can keep is 30, so the old log file which is more than 30 will get deleted.
Now here we have to keep 100 files, then set the retain value to 100.
$ sudo pm2 set pm2-logrotate:retain 100
How to do log rotation with date.
So now set the date on the log files to identify what is the date of the logs.
And if anyone says Hey! yesterday the application got crashed, so we will be able to identify the yesterday logs.
$ sudo pm2 set pm2-logrotate:dateFormat YYYY-MM-DD
YYYY is the year, MM is the month and DD is the day which will be concat with the logs while rotating.
How to logrotate daily via PM2-logrotate.
Now if we want to logrotate daily at 10:00 AM. So we will set rotateInterval to 10:00 AM.
It is the same as crontab, we can set the rotateInterval to weekly, and hourly as well.
$ sudo pm2 set pm2-logrotate:rotateInterval '00 10 * * *'
As you can see the * on the rotateInterval, So the 1st * is for the seconds which is optional.
2nd * is for minutes, if you want to rotate the logs every 5 minutes use ‘*/5 * * * *’
The 3rd * is for an hour. Suppose you have to rotate the log every 2 hours then the parameter will look like this ‘0 */2 * * *’
4th * is for a day of the Month, Suppose you want to rotate logs on 12:00 AM only on the 10th day of the month. Like 10th Jan, 10 Feb, 10 Mar, and so on. Now the parameter will look like ‘0 0 10 * *’
5th * is for a month, Now If you want to logrotate only on 12:00 AM 10 January. Then the parameter will be ‘0 0 10 1 *’. Keep note that on any other month this will not work.
6th * is for the day of the week, Suppose you want to logrotate every Saturday at 12:00 AM, then the parameter will be like ’00 00 * * 6′.