@abtnode/winston-daily-rotate-file

中文文档
5.0.1发布于 2024/06/29MITTypeScriptCommonJS近 30 天下载 2292

A transport for winston which logs to a rotating file each day.

原始使用文档
文档目录7 节
  1. Compatibility
  2. Install
  3. Options
  4. Usage
  5. ES6
  6. Typescript
  7. LICENSE

winston-daily-rotate-file

NPM version

NPM

A transport for winston which logs to a rotating file. Logs can be rotated based on a date, size limit, and old logs can be removed based on count or elapsed days.

Starting with version 2.0.0, the transport has been refactored to leverage the file-stream-rotator module. Some of the options in the 1.x versions of the transport have changed. Please review the options below to identify any changes needed.

Compatibility

Please note that if you are using winston@2, you will need to use winston-daily-rotate-file@3. winston-daily-rotate-file@4 removed support for winston@2.

Starting with version 5.0.0 this module also emits an "error" event for all low level filesystem error cases. Make sure to listen for this event to prevent crashes in your application.

This library should work starting with Node.js 8.x, but tests are only executed for Node.js 14+. Use on your own risk in lower Node.js versions.

Install

npm install winston-daily-rotate-file

Options

The DailyRotateFile transport can rotate files by minute, hour, day, month, year or weekday. In addition to the options accepted by the logger, winston-daily-rotate-file also accepts the following options:

  • frequency: A string representing the frequency of rotation. This is useful if you want to have timed rotations, as opposed to rotations that happen at specific moments in time. Valid values are '#m' or '#h' (e.g., '5m' or '3h'). Leaving this null relies on datePattern for the rotation times. (default: null)
  • datePattern: A string representing the moment.js date format to be used for rotating. The meta characters used in this string will dictate the frequency of the file rotation. For example, if your datePattern is simply 'HH' you will end up with 24 log files that are picked up and appended to every day. (default: 'YYYY-MM-DD')
  • zippedArchive: A boolean to define whether or not to gzip archived log files. (default: 'false')
  • filename: Filename to be used to log to. This filename can include the %DATE% placeholder which will include the formatted datePattern at that point in the filename. (default: 'winston.log.%DATE%')
  • dirname: The directory name to save log files to. (default: '.')
  • stream: Write directly to a custom stream and bypass the rotation capabilities. (default: null)
  • maxSize: Maximum size of the file after which it will rotate. This can be a number of bytes, or units of kb, mb, and gb. If using the units, add 'k', 'm', or 'g' as the suffix. The units need to directly follow the number. (default: null)
  • maxFiles: Maximum number of logs to keep. If not set, no logs will be removed. This can be a number of files or number of days. If using days, add 'd' as the suffix. It uses auditFile to keep track of the log files in a json format. It won't delete any file not contained in it. It can be a number of files or number of days (default: null)
  • options: An object resembling https://nodejs.org/api/fs.html#fs_fs_createwritestream_path_options indicating additional options that should be passed to the file stream. (default: { flags: 'a' })
  • auditFile: A string representing the path of the audit file, passed directly to file-stream-rotator as audit_file. If not specified, a file name is generated that includes a hash computed from the options object, and uses the dirname option value as the directory. (default: <dirname>/.<optionsHash>-audit.json)
  • utc: Use UTC time for date in filename. (default: false)
  • extension: File extension to be appended to the filename. (default: '')
  • createSymlink: Create a tailable symlink to the current active log file. (default: false)
  • symlinkName: The name of the tailable symlink. (default: 'current.log')
  • auditHashType: Use specified hashing algorithm for audit. (default: 'sha256')
  • level: Name of the logging level that will be used for the transport, if not specified option from createLogger method will be used

Usage

  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!');

using multiple transports

  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

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!');

This transport emits the following custom events:

  • new: fired when a new log file is created. This event will pass one parameter to the callback (newFilename).
  • rotate: fired when the log file is rotated. This event will pass two parameters to the callback (oldFilename, newFilename).
  • archive: fired when the log file is archived. This event will pass one parameter to the callback (zipFilename).
  • logRemoved: fired when a log file is removed from the file system. This event will pass one parameter to the callback (removedFilename).
    • error: fired when a low level filesystem error happens (e.g. EACCESS)

LICENSE

MIT

AUTHOR: Charlie Robbins
MAINTAINER: Matt Berther

版本与发布记录

@abtnode/winston-daily-rotate-file 共发布 2 个版本,首次发布于 2024/06/18;过去 12 个月发布了 0 个版本。

版本发布日期距今
5.0.1latest2024/06/292 年前
5.0.02024/06/182 年前

关于 @abtnode/winston-daily-rotate-file 的常见问题

@abtnode/winston-daily-rotate-file 是什么?

A transport for winston which logs to a rotating file each day.

如何安装 @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 和提交记录判断。