pm2和pm2-logrotate 日志管理 初探

pm2和pm2-logrotate 日志管理 初探

官網(wǎng):https://pm2.keymetrics.io/

ADVANCED, PRODUCTION PROCESS MANAGER FOR NODE.JS

高級(jí),Node.js生產(chǎn)環(huán)境進(jìn)程守護(hù)程序。

0x1 安裝

npm install pm2@latest -g

0x2 基本命令

$ pm2 start app.js -i 4  # 后臺(tái)運(yùn)行pm2,啟動(dòng)4個(gè)app.js 
                         # 也可以把'max' 參數(shù)傳遞給 start
                         # 正確的進(jìn)程數(shù)目依賴于Cpu的核心數(shù)目
$ pm2 start app.js -i max    # 根據(jù)有效CPU數(shù)目啟動(dòng)最大進(jìn)程數(shù)目
$ pm2 start app.js --name my-api # 命名進(jìn)程
$ pm2 list               # 顯示所有進(jìn)程狀態(tài)
$ pm2 monit              # 監(jiān)視所有進(jìn)程
$ pm2 logs               # 顯示所有進(jìn)程日志
$ pm2 stop all           # 停止所有進(jìn)程
$ pm2 restart all        # 重啟所有進(jìn)程
$ pm2 reload all         # 0 秒停機(jī)重載進(jìn)程 (用于 NETWORKED 進(jìn)程)
$ pm2 stop 0             # 停止指定的進(jìn)程
$ pm2 restart 0          # 重啟指定的進(jìn)程
$ pm2 startup            # 產(chǎn)生 init 腳本 保持進(jìn)程活著
$ pm2 web                # 運(yùn)行健壯的 computer API endpoint (http://localhost:9615)
$ pm2 delete 0           # 殺死指定的進(jìn)程
$ pm2 delete all         # 殺死全部進(jìn)程
$ pm2 info app           # 參看name為app的信息

日志管理

安裝pm2-logrotate

pm2的日志模塊默認(rèn)是每一個(gè)服務(wù)進(jìn)程都分配兩個(gè)默認(rèn)的日志文件

這兩個(gè)日志文件存放于/root/.pm2/logs中,如果pm2管理5個(gè)服務(wù),那么該文件夾下總共有10個(gè)日志文件,并且隨著時(shí)間不斷增加,很容易就會(huì)產(chǎn)生很多個(gè)上g的日志文件,導(dǎo)致了服務(wù)器的磁盤空間不足的問題

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, 10K
    • 配置項(xiàng)默認(rèn)是 10MB,并不意味著切割出來的日志文件大小一定就是 10MB,而是檢查時(shí)發(fā)現(xiàn)日志文件大小達(dá)到 max_size,則觸發(fā)日志切割。
  • 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.
    • 這個(gè)數(shù)字是在任何一個(gè)時(shí)間保留已分割的日志的數(shù)量,這意味著如果您保留7個(gè),那么您將最多有7個(gè)已分割日志和您當(dāng)前的一個(gè)
  • compress (Defaults to false): Enable compression via gzip for all rotated logs
    • 對(duì)所有已分割的日志啟用 gzip 壓縮
  • dateFormat (Defaults to YYYY-MM-DD_HH-mm-ss) : Format of the data used the name the file of log
    • 文件名格式化的規(guī)則
  • rotateModule (Defaults to true) : Rotate the log of pm2's module like other apps
    • 像其他應(yīng)用程序一樣分割 pm2模塊的日志
  • workerInterval (Defaults to 30 in secs) : You can control at which interval the worker is checking the log's size (minimum is 1)
    • 您可以控制工作線程檢查日志大小的間隔(最小值為1)單位為秒(控制模塊檢查log日志大小的循環(huán)時(shí)間,默認(rèn)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 :
    • 多久備份一次
*    *    *    *    *    *
┬    ┬    ┬    ┬    ┬    ┬
│    │    │    │    │    |
│    │    │    │    │    └ 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.
    • 時(shí)區(qū)(默認(rèn)為系統(tǒng)時(shí)區(qū))

如何設(shè)置 ?

安裝完模塊后,您必須鍵入: 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)

我的設(shè)置

$ pm2 set pm2-logrotate:max_size 10M
$ pm2 set pm2-logrotate:dateFormat YYYY-MM-DD_HH-mm-ss # 文件名時(shí)間格式
$ pm2 set pm2-logrotate:workerInterval 3600
$ pm2 set pm2-logrotate:rotateInterval 0 0 * * *
$ pm2 set pm2-logrotate:TZ Asia/Shanghai # 中國時(shí)區(qū)

參考:http://www.itdecent.cn/p/54bc346d2406 作者:kelvv

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容