# @eggjs/logrotator 中文文档

规范地址：https://npmcn.com/package/%40eggjs/logrotator/

版本：5\.1\.0
npm 元数据更新时间：2026-10-07T08:27:20.834Z
中文译文更新时间：2026-11-13T10:00:01.348Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40eggjs/logrotator
原文对照：https://npmcn.com/package/%40eggjs/logrotator/?lang=original
项目主页：https://github.com/eggjs/egg/tree/next/plugins/logrotator#readme
源码仓库：https://github.com/eggjs/egg

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：30.3 kB，38 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>= 22\.18\.0
- 运行依赖：2 个
- peer 依赖：egg 4\.2\.0
- 首次发布：2025-02-03T05:10:17.144Z
- 最新版本发布：2026-10-07T08:27:20.314Z
- 近 30 天下载量：4791（2026-10-13 至 2026-11-11，npm 官方下载统计）
- 已发布版本数：54（近 12 个月 33 个）

## 中文简介

鸡蛋的日志

## 安装

```sh
npm install @eggjs/logrotator
```

也可以使用：`pnpm add @eggjs/logrotator`、`yarn add @eggjs/logrotator`、`bun add @eggjs/logrotator`

## README

# @eggjs/logrotator

[![NPM version](https://img.shields.io/npm/v/@eggjs/logrotator.svg?style=flat-square)](https://npmjs.org/package/@eggjs/logrotator) [![npm download](https://img.shields.io/npm/dm/@eggjs/logrotator.svg?style=flat-square)](https://npmjs.org/package/@eggjs/logrotator) [![Node.js Version](https://img.shields.io/node/v/@eggjs/logrotator.svg?style=flat)](https://nodejs.org/en/download/) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com/) ![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/security)

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

## 配置

-   `config.default.js`

```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` 来自定义切割。

```js
// 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](https://github.com/eggjs/egg/issues).

## License

[MIT](https://npmcn.com/package/%40eggjs/logrotator/LICENSE)

## Contributors

[![Contributors](https://contrib.rocks/image?repo=eggjs/egg)](https://github.com/eggjs/egg/graphs/contributors)

Made with [contributors-img](https://contrib.rocks/).

## 常见问题

### @eggjs/logrotator 是什么？

鸡蛋的日志

### 如何安装 @eggjs/logrotator？

在项目目录运行 npm install @eggjs/logrotator，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：egg（4\.2\.0）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @eggjs/logrotator 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @eggjs/logrotator 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @eggjs/logrotator 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>= 22\.18\.0。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @eggjs/logrotator 可以商用吗？

@eggjs/logrotator 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @eggjs/logrotator 的下载量有多少？

根据 npm 官方下载统计，@eggjs/logrotator 在 2026/10/13 – 2026/11/11 这 30 天内被下载 4,791 次，日均约 160 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @eggjs/logrotator 还在维护吗？

最新版本 5\.1\.0 发布于 2026/10/07（1 个月前）；至今共发布 54 个版本，其中过去 12 个月发布了 33 个；首次发布于 2025/02/03。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

