Node log management / PM2 logrotate ext log management


This article is about how to use PM2 to manage the log output (cutting and backup) of node projects. The steps in this article are based on the premise that PM2 has been installed. If there is no PM2, please do it yourself.


Step 1: you need to log in to the company server, check all the logs saved by the current server (for comparison with the following effects), and execute the command PM2 logs to view it.

Step 2: install the PM2 log cutting management tool (the tool is an extension of PM2 logrotate), and execute the command: PM2 install PM2 logrotate ext

Step 3: after installation, the system will restart automatically. You can execute the command to set the following parameters. After setting the parameters, the system will restart automatically

pm2 set pm2-logrotate- ext:retain 7 / / save 7 files

pm2 set pm2-logrotate- ext:compress false / / start compression

pm2 set pm2-logrotate- ext:dateFormat YYYY-MM-DD_ HH mm SS / / log name adding time

pm2 set pm2-logrotate- ext:rotateInterval ‘0 0 0 * * *’ / / start backup at 0 am every day

pm2 set pm2-logrotate- ext:rotateModule true / / split the log of PM2 itself

pm2 set pm2-logrotate- ext:max_ Size 100M / / the size of each backup file is 100m

pm2 set pm2-logrotate- ext:workerInterval 5 / / set the size of the monitoring log for several work processes to be started. The default value is 30, which is set to 5 here

pm2 set pm2-logrotate- ext:retain 30 / / save 30 backup log files. If more than 30 backup log files are saved, the previous ones will be deleted to prevent the overall volume of the log from getting larger and larger



Step 4: after execution, you can view the current settings: PM2 conf PM2 logrotate ext

Step 5: if the previous log is not needed, you can use the command: PM2 flush to delete the log without restarting the project. You can modify the parameters to verify the backup results.


I only set the items marked with red in the above parameters, and the default configuration is reserved for other items,

The final effect is: if the log file of a project on that day exceeds the set size, which is 100m here, it will automatically cut and back up the current day file. If it does not, the backup will be started at 0:00 every day. Of course, the premise of backup is that there is a new log output, and no new log output will be backed up. A total of 30 backup logs are saved for each project.


This is my node project log management, if you have other good suggestions, you can leave a message to share.