Pm2 and PM2-Logrotate log management

Liverpoolfc.tv: pm2. Keymetrics. IO /

ADVANCED, PRODUCTION PROCESS MANAGER FOR NODE.JS

Advanced node.js production environment process daemon.

0 x1 installation

npm install pm2@latest -g
Copy the code

0x2 Basic Command

$ pm2 start app.js -i 4  # Background run PM2, start 4 app.js
                         It is also possible to pass the 'Max' argument to start
                         The correct number of processes depends on the number of cores in the Cpu
$ pm2 start app.js -i max    The maximum number of processes to start based on the number of available cpus
$ pm2 start app.js --name my-api # name process
$ pm2 list               Display all process status
$ pm2 monit              # Monitor all processes
$ pm2 logs               Display all process logs
$ pm2 stop all           # Stop all processes
$ pm2 restart all        Restart all processes
$ pm2 reload all         Stop overloaded processes in 0 seconds (for NETWORKED processes)
$ pm2 stop 0             Stop the specified process
$ pm2 restart 0          Restart the specified process
$ pm2 startup            Create init script to keep the process alive
$ pm2 web                # Run a robust Computer API endpoint (http://localhost:9615)
$ pm2 delete 0           Kill the specified process
$ pm2 delete all         # Kill all processes
$ pm2 info app           # see information whose name is app
Copy the code

Log management

Install the pm2 – logrotate

The pM2 log module defaults to two default log files for each server process

These two log files are stored in /root/.pm2/logs. If PM2 manages 5 services, there are 10 log files in this folder. As time goes on, it is easy to create many log files with more than gb, causing disk space shortage on the server

pm2 install pm2-logrotate
Copy the code

configuration

  • max_size (Defaults to 10M) : When a file size becomes higher than this value it will rotate it (its possible that the worker check the file after it actually pass the limit) . You can specify the unit at then end:10G.10M.10K
    • The default configuration item is 10MB. This does not mean that the size of the log file must be 10MB. If the size of the log file reaches max_size, log cutting is triggered.
  • retain (Defaults to 30 file logs): This number is the number of rotated logs that are keep at any one time, it means that if you have retain = 7 you will have at most 7 rotated logs and your current one.
    • This number is the number of partitioned logs kept at any one time, which means that if you keep seven, you will have up to seven partitioned logs and your current one
  • compress (Defaults to false): Enable compression via gzip for all rotated logs
    • Enable gzip compression for all partitioned logs
  • dateFormat (Defaults to YYYY-MM-DD_HH-mm-ss) : Format of the data used the name the file of log
    • Rules for formatting file names
  • rotateModule (Defaults to true) : Rotate the log of pm2’s module like other apps
    • Split the PM2 module’s logs like any other application
  • workerInterval (Defaults to 30 in secs) : You can control at which interval the worker is checking the log’s size (minimum is 1)
    • You can control the interval at which the worker thread checks the log size (the minimum is1) Unit: second (Cyclic time for the control module to check the size of the log. By default, the control module checks the size of the log every 30 seconds.)
  • rotateInterval (Defaults to 0 0 * * * everyday at midnight): This cron is used to a force rotate when executed. We are using node-schedule to schedule cron, so all valid cron for node-schedule is valid cron for this option. Cron style :
    • How often to back up
* * * * * * ┬ ┬ ┬ ┬ ┬ ┬ │ │ │ │ │ | │ │ │ │ │ └ day of week (0 to 7) (0 or 7 is Sun │ │ │ │ └ ─ ─ ─ ─ ─ the month (1-12) │ │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ day of the month (1-31) │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ the adrenaline-charged (0-23) │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ minute (0-59) └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ the second (0 to 59, OPTIONAL)Copy the code
  • TZ (Defaults to system time): This is the standard tz database timezone used to offset the log file saved. For instance, a value of Etc/GMT+1, with an hourly log, will save a file at hour 14 GMT with hour 13(GMT+1) in the log name.
    • Time zone (system time zone by default)

How do I set it?

After installing the module, you must type: pm2 set pm2-logrotate:

Such as:

  • pm2 set pm2-logrotate:max_size 1K (1KB)
  • pm2 set pm2-logrotate:compress true (compress logs when rotated)
  • pm2 set pm2-logrotate:rotateInterval '*/1 * * * *' (force rotate every minute)

I set up

$ pm2 set pm2-logrotate:max_size 10M
$ pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss # File name time format
$ pm2 set pm2-logrotate:workerInterval 3600
$ pm2 set pm2-logrotate:rotateInterval 0 0 * * *
$ pm2 set pm2-logrotate:TZ Asia/Shanghai # China time zone
Copy the code

Reference: www.jianshu.com/p/54bc346d2… Author: kelvv