# @abtnode/winston-daily-rotate-file 中文文档

规范地址：https://npmcn.com/package/%40abtnode/winston-daily-rotate-file/

版本：5\.0\.1
npm 元数据更新时间：2024-06-28T21:38:02.847Z
中文译文更新时间：2026-11-06T21:55:47.442Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/%40abtnode/winston-daily-rotate-file
原文对照：https://npmcn.com/package/%40abtnode/winston-daily-rotate-file/?lang=original
项目主页：https://github.com/winstonjs/winston-daily-rotate-file#readme
源码仓库：https://github.com/winstonjs/winston-daily-rotate-file

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

- 包体积（解压后）：27.3 kB，6 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=8
- 运行依赖：4 个
- peer 依赖：winston ^3
- 首次发布：2024-06-18T08:08:42.946Z
- 最新版本发布：2024-06-28T21:38:02.629Z
- 近 30 天下载量：2292（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：2（近 12 个月 0 个）

## 中文简介

用于 Winton 的传输, 每天登录到旋转文件 .

## 安装

```sh
npm install @abtnode/winston-daily-rotate-file
```

也可以使用：`pnpm add @abtnode/winston-daily-rotate-file`、`yarn add @abtnode/winston-daily-rotate-file`、`bun add @abtnode/winston-daily-rotate-file`

## README

# winton - 每日轮换文件

[![NPM version](https://badge.fury.io/js/winston-daily-rotate-file.svg)](https://npmjs.org/package/winston-daily-rotate-file)

[![NPM](https://nodei.co/npm/winston-daily-rotate-file.png)](https://nodei.co/npm/winston-daily-rotate-file/)

运输 [温通](https://github.com/winstonjs/winston) 输入旋转文件。 日志可以根据日期,大小限制来旋转,而旧日志可以根据计数日或已过期日来取出.

从2.0版本开始,运输器被重置,以利用 [文件流旋转器](https://github.com/rogerc/file-stream-rotator/) 模块. _1.x版本运输的一些选项已经改变._ 请审查以下备选办法,以确定需要作哪些修改.

## 兼容性

请注意,如果使用 `winston@2`,您需要使用 `winston-daily-rotate-file@3`。 。 。 . `winston-daily-rotate-file@4` 删除支持 `winston@2`。 。 。 .

从5.0.0版本开始,本模块也对所有低级文件系统出错案例发出"error"事件. 请为此事件收听, 以防止您的应用程序中发生相撞 .

这个库应该从Node.js 8.x开始工作,但测试只针对Node.js 14+执行. 在下节点.js版本中自担风险使用.

## 安装

```
npm install winston-daily-rotate-file
```

## 选项

Daily RotateFile运输可以按分钟,小时,日,月,年或工作日来旋转文件. 除了伐木工接受的选项外, `winston-daily-rotate-file` 还接受以下选项:

-   **频率 :** 表示旋转频率的字符串 。 如果您想要有定时旋转, 而不是在特定时刻发生的旋转, 这样做是有用的 。 有效值为"#m"或"#h"(如"5m"或"3h"). 离开这个虚无依赖 `datePattern` 用于旋转时间。 (默认: 无效)
-   **日期标签 :** 一个代表 [moment.j 日期格式](http://momentjs.com/docs/#/displaying/format/) 用于旋转。 此字符串中使用的元字符会决定文件旋转的频率. 例如, 如果您的datePattern 仅仅是“ HH ” , 则您终会收到24个日志文件, 这些文件每天被取出并附上 。 (默认:"YYY-MM-DD")
-   **已拉链箭头 :** 用于定义 gzip 存档日志文件是否使用的布尔 。 (默认 : “ 假” )
-   **文件名 :** 要登录的文件名 。 此文件名可以包含 `%DATE%` 占位符,其中将包含文件名中此时格式化的日期样式。 (默认:"winston.log. % 日期%')
-   **目录 :** 要保存日志文件的目录名称 。 (默认:'.')
-   **流线 :** 直接写入自定义流并绕过自转能力. (默认: 无效)
-   **最大尺寸 :** 最大文件大小, 然后它会旋转 。 这可以是数个字节,或kb,mb,和gb的单位. 如果使用这些单位,则添加“k”、“m”或“g”作为后缀。 各单位需要直接跟随编号. (默认: 无效)
-   **最大文件夹 :** 要保存的最大日志数 。 如果不设定, 将不会删除日志 。 这可以是多个文件或天数. 如果使用天数,则添加"d"作为后缀. 它使用审计文件来以json格式跟踪日志文件. 它不会删除任何没有包含在其中的文件. 可以是多个文件或天数( 默认: 无效)
-   **选项:** 类似对象 [https://nodejs.org/api/fs.html#fs fs createwritestream path options 互联网档案馆的存檔,存档日期2013-12-22](https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options) 显示应当传递到文件流的其他选项。 (默认 : `{ flags: 'a' }`(中文(简体) )
-   **审计文件**: 代表审计文件路径的字符串,直接传递给 [文件流旋转器](https://github.com/rogerc/file-stream-rotator/) 作为 `audit_file`如果未指定,则生成一个文件名,包括从选项对象计算出来的散列,并使用 `dirname` 选项值作为目录。 (默认 : `<dirname>/.<optionsHash>-audit.json`(中文(简体) )
-   **关卡**: 使用世界协调时的时间进行文件名中的日期。 (默认:虚假)
-   **扩展**: 文件扩展名将附加在文件名上。 (默认:\`')
-   **创建系统链接**: 创建与当前活动日志文件的可尾可并联链接. (默认:虚假)
-   **符号链接Name**:可被尾接的同音链接的名称. (默认 : “ 当前.log ” )
-   **审计 HashType**: 使用指定的散列算法进行审计。 (默认:"sha256")
-   **职等**: 用于运输的伐木层名称,如果未指定从 `createLogger` 将使用方法

## 使用量

```js
  var winston = require('winston');
  require('winston-daily-rotate-file');

  var transport = new winston.transports.DailyRotateFile({
    level: 'info',
    filename: 'application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
  });
  
  transport.on('error', error => {
    // log or handle errors here
  });

  transport.on('rotate', (oldFilename, newFilename) => {
    // do something fun
  });

  var logger = winston.createLogger({
    transports: [
      transport
    ]
  });

  logger.info('Hello World!');

```

使用多路运输

```js
  var winston = require('winston');
  require('winston-daily-rotate-file');

  var transport1 = new winston.transports.DailyRotateFile({
    filename: 'application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
  });

  var transport2 = new winston.transports.DailyRotateFile({
    level: 'error',
    filename: 'application-error-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
  });

  transport1.on('error', error => {
    // log or handle errors here
  });

  transport2.on('error', error => {
    // log or handle errors here
  });

  transport1.on('rotate', function(oldFilename, newFilename) {
    // do something fun
  });

  transport2.on('rotate', function(oldFilename, newFilename) {
    // do something fun
  });

  var logger = winston.createLogger({
    level: 'info'
    transports: [
      transport1, // will be used on info level
      transport2  // will be used on error level
    ]
  });

  logger.info('Hello World!');
  logger.error('Hello Error!');

```

### ES6 标准

```js
import  *  as  winston  from  'winston';
import  'winston-daily-rotate-file';


const transport = new winston.transports.DailyRotateFile({
  filename: 'application-%DATE%.log',
  datePattern: 'YYYY-MM-DD-HH',
  zippedArchive: true,
  maxSize: '20m',
  maxFiles: '14d'
});

transport.on('error', error => {
  // log or handle errors here
});

transport.on('rotate', (oldFilename, newFilename) => {
  // do something fun
});

const logger = winston.createLogger({
  transports: [
    transport
  ]
});

logger.info('Hello World!');
```

### 字体

```typescript

import * as winston from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';

const transport: DailyRotateFile = new DailyRotateFile({
    filename: 'application-%DATE%.log',
    datePattern: 'YYYY-MM-DD-HH',
    zippedArchive: true,
    maxSize: '20m',
    maxFiles: '14d'
});

transport.on('error', error => {
    // log or handle errors here
});


transport.on('rotate', (oldFilename, newFilename) => {
    // do something fun
});

const logger = winston.createLogger({
    transports: [
        transport
    ]
});

logger.info('Hello World!');
```

此运算显示以下自定义事件:

-   **新设**: 当创建了新日志文件时开火 。 此事件将传递一个参数到回调(_新文件名称_) (中文(简体) ).
-   **旋转**:当日志文件被旋转时开火. 此事件将传递两个参数到召回( Name_旧文件名_, (中文(简体) ), _新文件名称_) (中文(简体) ).
-   **归档**: 日志文件存档时已开火 。 此事件将传递一个参数到回调(_拉链文件名_) (中文(简体) ).
-   **日志已删除**:当一个日志文件从文件系统中被删除时被发射. 此事件将传递一个参数到回调(_已删除文件名_) (中文(简体) ).
-   -   **错误**: 当发生低级别文件系统错误时开火( 如 EACCESS)

## 自由主义

麻省理工学院

##### 授权: [查理·罗宾斯](https://github.com/indexzero)

##### 矿工: [马特·伯瑟](https://github.com/mattberther)

## 常见问题

### @abtnode/winston-daily-rotate-file 是什么？

用于 Winton 的传输, 每天登录到旋转文件 \.

### 如何安装 @abtnode/winston-daily-rotate-file？

在项目目录运行 npm install @abtnode/winston-daily-rotate-file，或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖：winston（^3）。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖，Yarn 需要手动安装。

### @abtnode/winston-daily-rotate-file 支持 TypeScript 吗？

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

### @abtnode/winston-daily-rotate-file 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @abtnode/winston-daily-rotate-file 需要什么版本的 Node\.js？

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

### @abtnode/winston-daily-rotate-file 可以商用吗？

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

### @abtnode/winston-daily-rotate-file 的下载量有多少？

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

### @abtnode/winston-daily-rotate-file 还在维护吗？

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

