pm2和pm2-logrotate 日誌管理 初探

pm2和pm2-logrotate 日誌管理 初探

官網:https://pm2.keymetrics.io/node

ADVANCED, PRODUCTION PROCESS MANAGER FOR NODE.JS

高級,Node.js生產環境進程守護程序。git

0x1 安裝

npm install pm2@latest -g

0x2 基本命令

$ pm2 start app.js -i 4  # 後臺運行pm2,啓動4個app.js 
                         # 也能夠把'max' 參數傳遞給 start
                         # 正確的進程數目依賴於Cpu的核心數目
$ pm2 start app.js -i max    # 根據有效CPU數目啓動最大進程數目
$ pm2 start app.js --name my-api # 命名進程
$ pm2 list               # 顯示全部進程狀態
$ pm2 monit              # 監視全部進程
$ pm2 logs               # 顯示全部進程日誌
$ pm2 stop all           # 中止全部進程
$ pm2 restart all        # 重啓全部進程
$ pm2 reload all         # 0 秒停機重載進程 (用於 NETWORKED 進程)
$ pm2 stop 0             # 中止指定的進程
$ pm2 restart 0          # 重啓指定的進程
$ pm2 startup            # 產生 init 腳本 保持進程活着
$ pm2 web                # 運行健壯的 computer API endpoint (http://localhost:9615)
$ pm2 delete 0           # 殺死指定的進程
$ pm2 delete all         # 殺死所有進程
$ pm2 info app           # 參看name爲app的信息

日誌管理

安裝pm2-logrotate

pm2的日誌模塊默認是每個服務進程都分配兩個默認的日誌文件

這兩個日誌文件存放於/root/.pm2/logs中,若是pm2管理5個服務,那麼該文件夾下總共有10個日誌文件,而且隨着時間不斷增長,很容易就會產生不少個上g的日誌文件,致使了服務器的磁盤空間不足的問題github

pm2 install pm2-logrotate

配置

  • 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, 10Kweb

    • 配置項默認是 10MB,並不意味着切割出來的日誌文件大小必定就是 10MB,而是檢查時發現日誌文件大小達到 max_size,則觸發日誌切割。
  • 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.npm

    • 這個數字是在任何一個時間保留已分割的日誌的數量,這意味着若是您保留7個,那麼您將最多有7個已分割日誌和您當前的一個
  • compress (Defaults to false): Enable compression via gzip for all rotated logsapi

    • 對全部已分割的日誌啓用 gzip 壓縮
  • dateFormat (Defaults to YYYY-MM-DD_HH-mm-ss) : Format of the data used the name the file of logbash

    • 文件名格式化的規則
  • rotateModule (Defaults to true) : Rotate the log of pm2's module like other apps服務器

    • 像其餘應用程序同樣分割 pm2模塊的日誌
  • workerInterval (Defaults to 30 in secs) : You can control at which interval the worker is checking the log's size (minimum is 1)app

    • 您能夠控制工做線程檢查日誌大小的間隔(最小值爲1)單位爲秒(控制模塊檢查log日誌大小的循環時間,默認30s檢查一次)
  • 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 :this

    • 多久備份一次
*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ day of week (0 - 7) (0 or 7 is Sun)
│    │    │    │    └───── month (1 - 12)
│    │    │    └────────── day of month (1 - 31)
│    │    └─────────────── hour (0 - 23)
│    └──────────────────── minute (0 - 59)
└───────────────────────── second (0 - 59, OPTIONAL)
  • 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.

    • 時區(默認爲系統時區)

如何設置 ?

安裝完模塊後,您必須鍵入: pm2 set pm2-logrotate:<param> <value>

例如:

  • 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)

個人設置

$ pm2 set pm2-logrotate:max_size 10M
$ pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss # 文件名時間格式
$ pm2 set pm2-logrotate:workerInterval 3600
$ pm2 set pm2-logrotate:rotateInterval 0 0 * * *
$ pm2 set pm2-logrotate:TZ Asia/Shanghai # 中國時區
參考: https://www.jianshu.com/p/54bc346d2406 做者:kelvv
相關文章
相關標籤/搜索