egg-logrotator
logrotator for egg
Last updated 2 years ago by fengmk2 .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ npm install egg-logrotator 
SYNC missed versions from official npm registry.

egg-logrotator

NPM version CI Test coverage npm download

egg 的日志切割插件,默认会按照时间切割所有的 app.loggers。

配置

  • plugin.js
exports.logrotator = true;
  • config.default.js
// 如果有需要按照文件大小切割的日志,在这里配置
exports.logrotator = {
  filesRotateByHour: [],           // 需要按小时切割的文件
  hourDelimiter: '-',              // 按照小时切割的文件, 小时部分的分隔符.
  filesRotateBySize: [],           // 需要按大小切割的文件,其他日志文件仍按照通常方式切割
  maxFileSize: 50 * 1024 * 1024,   // 最大文件大小,默认为50m
  maxFiles: 10,                    // 按大小切割时,文件最大切割的份数
  rotateDuration: 60000,           // 按大小切割时,文件扫描的间隔时间
  maxDays: 31,                     // 日志保留最久天数
};

功能说明

logrotator 默认在每日0点按照时间切割,会将 app.loggers 下所有的 logger 都进行切割,格式为 .log.YYYY-MM-DD,如 egg-web.log.2016-09-30。

按大小切割

可以配置 filesRotateBySize 文件列表按大小切割,当文件大于 maxFileSize 时进行切割,格式为 .log.1。

当已有切割文件时会将原文件自增 1,如 .log.1 -> .log.2。当切割分数大于 maxFiles 时会覆盖最后一份。

配置了这个功能的文件不会再按默认切割。

如配置为相对路径,则默认会转换为 path.join(this.app.config.logger.dir, file)。

按小时切割

可以配置 filesRotateBySize 文件列表按小时切割,每小时0分开始切割,格式为 .log.YYYY-MM-DD-HH。

配置了这个功能的文件不会再按默认切割。

如配置为相对路径,则默认会转换为 path.join(this.app.config.logger.dir, file)。

自定义

你可以使用 app.LogRotator 来自定义切割。

// app/schedule/custom.js
module.exports = app => {
  const rotator = getRotator(app);
  return {
    // https://github.com/eggjs/egg-schedule
    schedule: {
      type: 'worker', // only one worker run this task
      cron: '10 * * * *', // custom cron, or use interval
    },
    * task() {
      yield rotator.rotate();
    }
  };
};

function getRotator(app) {
  class CustomRotator extends app.LogRotator {
    // return map that contains a pair of srcPath and targetPath
    // LogRotator will rename srcPath to targetPath
    // 返回一个 map,其中包含 srcPath 和 targetPath,
    // LogRotator 会将 srcPath 重命名成 targetPath
    * getRotateFiles() {
      const files = new Map();
      const srcPath = '/home/admin/foo.log';
      const targetPath = '/home/admin/foo.log.2016.09.30';
      files.set(srcPath, { srcPath, targetPath });
      return files;
    }
  }
  return new CustomRotator({ app });
}

你只需要定义一个 getRotateFiles 方法,指定重命名的 map。

Questions & Suggestions

Please open an issue here.

License

MIT

Contributors

Contributors

Made with contributors-img.

Current Tags

  • 3.2.0                                ...           latest (2 years ago)
  • 2.3.3                                ...           latest-2 (8 years ago)
  • 3.2.0                                ...           release-3.x (2 years ago)

20 Versions

  • 3.2.0                                ...           2 years ago
  • 3.1.0                                ...           8 years ago
  • 3.0.7                                ...           8 years ago
  • 3.0.6                                ...           8 years ago
  • 2.3.3                                ...           8 years ago
  • 3.0.5                                ...           8 years ago
  • 3.0.4                                ...           8 years ago
  • 2.3.2                                ...           9 years ago
  • 3.0.3                                ...           9 years ago
  • 3.0.2                                ...           9 years ago
  • 2.3.1                                ...           9 years ago
  • 3.0.1                                ...           9 years ago
  • 3.0.0                                ...           9 years ago
  • 2.3.0                                ...           9 years ago
  • 2.2.3                                ...           9 years ago
  • 2.2.2                                ...           10 years ago
  • 2.2.1                                ...           10 years ago
  • 2.2.0                                ...           10 years ago
  • 2.1.0                                ...           10 years ago
  • 2.0.0                                ...           10 years ago
Downloads
Total 84
Today 0
This Week 4
This Month 47
Last Day 0
Last Week 19
Last Month 22
Dependencies (2)
Dev Dependencies (9)
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE