npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

egg-logrotator

v3.2.0

Published

logrotator for egg

Downloads

88,925

Readme

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.