PM2 logrotate config file, daily, install, delete old logs

So, Today we will learn about PM2 logrotate install and config file of the PM2 logrotate daily, PM2 logrotate delete old logs.

Log management is one of the major concerns to the firms, let’s discuss some bullet point’s why the logs are important in the daily life of Developers, Sysadmins, and the DevOps Engineers.

QUICK view for PM2 Logrotate install and PM2 logrotate config.
How to Install PM2 logrotate

Command:
# pm2 install pm2-logrotate
How to check the default config of PM2 logrotate

Command:
# pm2 config pm2-logrotate
How to change the max_size of PM2 logrotate

Command:
# pm2 set pm2-logrotate:size 300M
**Once the log file reached the size of 300M then the pm2-logrotate will work** 
How to change the retain file in PM2 logrotate

Command:
# pm2 set pm2-logrotate:retain 50
**It will keep only 50 logfiles, If the 51st file will try to perform the logrtate then it will delete the olf log file.** 
How to compress the logs in PM2 logrotate

Command:
# pm2 set pm2-logrotate:compress true
**This will compress the log files**
How to Change the Date format of the logs in PM2 logrotate

Command:
# pm2 set pm2-logrotate:dateFormat YYYY-MM-DD
**So the date format will be year-month-day**
How to Change the rotate Interval time of the logs in PM2 logrotate.

Command:
# pm2 set pm2-logrotate:rotateInterval '00 10 * * *'
**We have set the log rotate Every day at 10:00 AM**
  • Find out the incoming request and response for the real-time events
  • Most importantly if the application got crashed first thing all the developers, DevOps engineer, and System admins to check the logs.
  • It is one of the most valuable information for the application as well as the server that is running 24*7.
  • The log should be in particular directories, the old logs should be compressed.
  • Also, we have to keep the logs as per our business needs and requirement.

We have seen the installation of Node in Linux, later we have seen the pm2 install in Linux, and before looking to pm2-logrotate config, we will learn how to set the node js logs path into the particular directory.

How to check the current logs?

$ sudo pm2 logs

So now we have understood if we are running two applications with the name “tla” and “tla2” then it will show the last 15 lines of both the application and also show the same of the error logs

/home/tla/.pm2/logs/tla2-out.log
last 15 lines:
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!
1|taste | Welcome Guys!

/home/tla/.pm2/logs/tla-out.log
last 15 lines:
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!
0|taste | Welcome to Taste The Linux!

/home/tla/.pm2/logs/tla-out.log last 15 lines: 0|taste | Welcome to Taste The Linux!

Now we can see the above logs in detail

  • /home/tla/.pm2/logs/tla-out.log this is Path of the log file where the logs are been stored
  • 0|taste | Welcome to Taste The Linux!
    • 0 means the id of the application.
    • taste is the application name.
    • The logs that are generated by the code which is Welcome to Taste The Linux!”.
Set 2 parameters at the start of pm2 the error log and the output logs.
  1. The default path of the logs.
  2. We want to set the timestamp of the logs from this we will get the time of that specific events.
$ sudo pm2 start tla.js tla2.js -o "/var/log/node/out.log" -e "/var/log/node/err.log" --log-date-format="YYYY-MM-DD HH:mm Z"

The -o is the output log which will be redirected into the file /var/log/node/out.log,

-e is the error log which will be redirected into the file /var/log/node/err.log

The –log-date-format=”YYYY-MM-DD HH:mm Z” is the format of the timestamp that we want into our logs.

Now you can see on the above logs there are timestamp as well as the path of the log files.

we have to use the logrotate in two ways we can write our own shell script or we can use pm2-logrotate.

Let’s look into pm2-logrotate Config
$ sudo pm2 install pm2-logrotate

NOTE: Don’t use npm install pm2-logrotate

pm2 logrotate, pm2 logrotate install, pm2 logrotate rotate interval, pm2 logrotate settings, pm2 npm, pm2 logrotate daily, pm2 logrotate not working, pm2 logrotate get a config, pm2 logrotate config file, pm2 logrotate delete old logs

There is a worker Interval which runs at every 30 sec into the pm2-logrotate and executes the configurations.

So, PM2 logrotate config has some of the default values, so now we will discuss 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, this value is 30, which means if the 31 files will be generated old log file will be deleted.
  • compress: By default, it is false, if want compression than set it to true.
  • dateFormat: YYYY-MM-DD_HH-mm-ss this is by default. This the date format.
  • rotateInterval: By default will set 0 0 * * * at everyday in midnight.

So, we can change the rotateInterval almost similar to a crontab in Linux, even more on pm2-logrotate offical.

— PM2 logrotate get config, config file

[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]:~$ 
$ sudo pm2 config pm2-logrotate

Above command, sudo pm2 config pm2-logrotate will show all the default configuration.

Now, what if we have to change the above configurations or look into the default configuration of the pm2-logrotate config.

[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]:~$

— PM2 logrotate compress

We have set the compression to true, so now we will find that the log was getting compressed.

$ sudo pm2 set pm2-logrotate:compress true

— PM2 logrotate size

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.

$ sudo pm2 set pm2-logrotate:size 20 M

— PM2 logrotate delete old logs

What if we have to keep only 50 log files, but default we can keep till 30 log files

$ sudo pm2 set pm2-logrotate:retain 50

— PM2 logrotate datetime

So now we will change the date format of the logs files

$ sudo pm2 set pm2-logrotate:dateFormat YYYY-MM-DD

— PM2 logrotate daily

Now we will set the PM2 log rotate daily via pm2-logrotate

$ sudo pm2 set pm2-logrotate:rotateInterval '00 10 * * *'

We have set the rotate log Interval of 10:00 AM

Thanks Friends for reading this Article! Share with your friends

pm2 logrotate, pm2 logrotate install, pm2 logrotate rotate interval, pm2 logrotate settings, pm2 npm, pm2 logrotate daily, pm2 logrotate not working, pm2 logrotate get a config, pm2 logrotate config file, pm2 logrotate delete old logs

4 Replies to “PM2 logrotate config file, daily, install, delete old logs

Give your valuable time