日志记录和日志分割

pm2介绍

生产环境的node管理工具

pm2日志

  • pm2默认输出日志路径

    1
    2
    错误日志:  /home/username/.pm2/logs/
    普通日志: /home/username/.pm2/logs/
  • 存在问题

    • 无法对日志文件进行切割
    • 清理日志文件需要手动触发

pm2-logrotate日志管理

安装pm2-logrotate

pm2 install pm2-logrotate

pm2-logrotate参数配置

  • max_size (Defaults to 10M): 单个日志文件的最大值,例如:10G, 10M, 10K

  • retain (Defaults to 30 file logs): 可以存储的最大日志文件数量, 例如为7时,表示可以保存7个最新的日志文件加当前使用的日志文件

  • compress (Defaults to false): 压缩存储的历史日志文件,不包括当前使用的日志文件

  • dateFormat (Defaults to YYYY-MM-DD_HH-mm-ss) : 日志文件名称格式

  • rotateModule (Defaults to true) : Rotate the log of pm2’s module like other apps

  • workerInterval (Defaults to 30 in secs) : 调用检查日志文件大小任务的定时器时间间隔,默认值为30秒,最小值为1秒

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

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    *    *    *    *    *    *
    ┬ ┬ ┬ ┬ ┬ ┬
    │ │ │ │ │ |
    │ │ │ │ │ └ 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)

参考文档