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

raynos-rotating-file-stream

v2.1.3-patch1

Published

Opens a stream.Writable to a file rotated by interval and/or size. A logrotate alternative.

Downloads

2

Readme

rotating-file-stream

Build Status Code Climate Test Coverage Donate

NPM version Types NPM downloads Stars

Dependencies Dev Dependencies Dependents

Description

Creates a stream.Writable to a file which is rotated. Rotation behaviour can be deeply customized; optionally, classical UNIX logrotate behaviour can be used.

Usage

const rfs = require("rotating-file-stream");
const stream = rfs.createStream("file.log", {
  size: "10M", // rotate every 10 MegaBytes written
  interval: "1d", // rotate daily
  compress: "gzip" // compress rotated files
});

Installation

With npm:

$ npm install --save rotating-file-stream

Table of contents

Upgrading from v1.x.x to v2.x.x

There are two main changes in package interface.

In v1 the default export of the packege was directly the RotatingFileStream constructor and the caller have to use it; while in v2 there is no default export and the caller should use the createStream exported function and should not directly use RotatingFileStream class. This is quite easy to discover: if this change is not applied, nothing than a runtime error can happen.

The other important change is the removal of option rotationTime and the introduction of intervalBoundary. In v1 the time argument passed to the filename generator function, by default, is the time when rotaion job started, while if options.interval option is used, it is the lower boundary of the time interval within rotaion job started. Later I was asked to add the possibility to restore the default value for this argument so I introduced options.rotationTime option with this purpose. At the end the result was something a bit confusing, something I never liked. In v2 the time argument passed to the filename generator function is always the time when rotaion job started, unless options.intervalBoundary option is used. In a few words, to maintain back compatibility upgrading from v1 to v2, just follow this rules:

API

const rfs = require("rotating-file-stream");

rfs.createStream(filename[, options])

This interface is inspired to fs.createWriteStream one. The file is rotated following options rules.

filename

The most complex problem about file name is: "how to call the rotated file name?"

The answer to this question may vary in many forms depending on application requirements and/or specifications. If there are no requirements, a string can be used and default rotated file name generator will be used; otherwise a Function which returns the rotated file name can be used.

Note: if part of returned destination path does not exists, the rotation job will try to create it.

filename(time[, index])

  • time <Date>

    If null, the not-rotated file name must be returned.

  • index <number> The progressive index of rotation by size in the same rotation period.

An example of a complex rotated file name generator function could be:

const pad = num => (num > 9 ? "" : "0") + num;
const generator = (time, index) => {
  if (!time) return "file.log";

  var month = time.getFullYear() + "" + pad(time.getMonth() + 1);
  var day = pad(time.getDate());
  var hour = pad(time.getHours());
  var minute = pad(time.getMinutes());

  return `${month}/${month}${day}-${hour}${minute}-${index}-file.log`;
};

const rfs = require("rotating-file-stream");
const stream = rfs(generator, {
  size: "10M",
  interval: "30m"
});

Note: if both of options.interval options.size are used, returned rotated file name must be function of both arguments time and index.

filename(index)

  • index <number> The progressive index of rotation. If null, the not-rotated file name must be returned.

If classical logrotate behaviour is enabled (by options.rotate), rotated file name is only a function of index.

Class: RotatingFileStream

Extends stream.Writable. It should not be directly used. Exported only to be used with instanceof operator and similar.

Event: 'history'

The history event is emitted once the history check job is completed.

Event: 'open'

The open event is emitted once the not-rotated file is opened.

Event: 'removed'

The removed event is emitted once a rotated file is removed due to options.maxFiles or options.maxSize.

Event: 'rotation'

The rotation event is emitted once the rotation job is started.

Event: 'rotated'

  • filename <string> The rotated file name produced.

The rotated event is emitted once the rotation job is completed.

Event: 'warning'

  • error <Error> The non blocking error.

The warning event is emitted once a non blocking error happens.

options

encoding

Specifies the default encoding that is used when no encoding is specified as an argument to stream.write().

mode

Proxied to fs.createWriteStream.

path

If present, it is prepended to generated file names as well as for history file.

teeToStdout

If true, it makes the file content to be written to stdout as well. Useful for debugging puposes.

size

Accepts a positive integer followed by one of these possible letters:

  • B: Bites
  • K: KiloBites
  • M: MegaBytes
  • G: GigaBytes
  size: '300B', // rotates the file when size exceeds 300 Bytes
                // useful for tests
  size: '300K', // rotates the file when size exceeds 300 KiloBytes
  size: '100M', // rotates the file when size exceeds 100 MegaBytes
  size: '1G', // rotates the file when size exceeds a GigaByte

interval

Accepts a positive integer followed by one of these possible letters:

  • s: seconds. Accepts integer divider of 60.
  • m: minutes. Accepts integer divider of 60.
  • h: hours. Accepts integer divider of 24.
  • d: days. Accepts integer.
  • M: months. Accepts integer. EXPERIMENTAL
  interval: '5s', // rotates at seconds 0, 5, 10, 15 and so on
                  // useful for tests
  interval: '5m', // rotates at minutes 0, 5, 10, 15 and so on
  interval: '2h', // rotates at midnight, 02:00, 04:00 and so on
  interval: '1d', // rotates at every midnight
  interval: '1M', // rotates at every midnight between two distinct months

intervalBoundary

If set to true, the argument time of filename generator is no longer the time when rotation job started, but the lower boundary of rotation interval.

Note: this option has effect only if options.interval is used.

initialRotation

When program stops in a rotation period then restarts in a new rotation period, logs of different rotation period will go in the next rotated file; in a few words: a rotation job is lost. If this option is set to true an initial check is performed against the not-rotated file timestamp and, if it falls in a previous rotation period, an initial rotation job is done as well.

Note: this option has effect only if options.intervalBoundary is used.

compress

For historical reasons external compression can be used, but the best choice is to use the value "gzip".

To enable external compression, a function can be used or simply the boolean true value to use default external compression. The function should accept source and dest file names and must return the shell command to be executed to compress the file. The two following code snippets have exactly the same effect:

var rfs = require("rotating-file-stream");
var stream = rfs("file.log", {
  size: "10M",
  compress: true
});
var rfs = require("rotating-file-stream");
var stream = rfs("file.log", {
  size: "10M",
  compress: (source, dest) => "cat " + source + " | gzip -c9 > " + dest
});

Note: this option is ignored if options.immutable is used.

Note: the shell command to compress the rotated file should not remove the source file, it will be removed by the package if rotation job complete with success.

initialRotation

When program stops in a rotation period then restarts in a new rotation period, logs of different rotation period will go in the next rotated file; in a few words: a rotation job is lost. If this option is set to true an initial check is performed against the not-rotated file timestamp and, if it falls in a previous rotation period, an initial rotation job is done as well.

Note: this option has effect only if both options.interval and options.intervalBoundary are used.

Note: this option is ignored if options.rotate is used.

rotate

If specified, classical UNIX logrotate behaviour is enabled and the value of this option has same effect in logrotate.conf file.

Note: if this optoin is used following ones take no effect: options.history, options.immutable, options.initialRotation, options.intervalBoundary, options.maxFiles, options.maxSize.

immutable

If set to true, names of generated files never changes. New files are immediately generated with their rotated name. In other words the rotated file name generator is never called with a null time argument unless to determinate the history file name; this can happen if options.history is not used while options.maxFiles or options.maxSize are used. The filename argument passed to 'open' event evaluates now as the newly created file name.

Useful to send logs to logstash through filebeat.

Note: if this option is used, options.compress is ignored.

Note: this option is ignored if options.interval is not used.

history

Due to the complexity that rotated file names can have because of the filename generator function, if number or size of rotated files should not exceed a given limit, the package needs a file where to store this information. This option specifies the name history file. This option takes effect only if at least one of options.maxFiles or options.maxSize is used. If null, the not rotated filename with the '.txt' suffix is used.

maxFiles

If specified, it's value is the maximum number of rotated files to be kept.

maxSize

If specified, it's value must respect same syntax of option.size and is the maximum size of rotated files to be kept.

Rotation logic

Regardless of when and why rotation happens, the content of a single stream.write will never be split among two files.

by size

Once the not-rotated file is opened first time, its size is checked and if it is greater or equal to size limit, a first rotation happens. After each stream.write, the same check is performed.

by interval

The package sets a Timeout to start a rotation job at the right moment.

Under the hood

Logs should be handled so carefully, so this package tries to never overwrite files.

At stream creation, if the not-rotated log file already exists and its size exceeds the rotation size, an initial rotation attempt is done.

At each rotation attempt a check is done to verify that destination rotated file does not exists yet; if this is not the case a new destination rotated file name is generated and the same check is performed before going on. This is repeated until a not existing destination file name is found or the package is exhausted. For this reason the rotated file name generator function could be called several times for each rotation job.

If requested through options.maxFiles or options.maxSize, at the end of a rotation job, a check is performed to ensure that given limits are respected. This means that while rotation job is running both the limits could be not respected. The same can happen till the end of first rotation job if options.maxFiles or options.maxSize are changed between two runs. The first check performed is the one against options.maxFiles, in case some files are removed, then the check against options.maxSize is performed, finally other files can be removed. When options.maxFiles or options.maxSize are enabled for first time, an history file can be created with one rotated filename (as returned by filename generator function) at each line.

Once an error event is emitted, nothing more can be done: the stream is closed as well.

Compatibility

Requires Node.js v10.x.

The package is tested under all Node.js versions currently supported accordingly to Node.js Release.

To work with the package under Windows, be sure to configure bash.exe as your script-shell.

> npm config set script-shell bash.exe

TypeScript

Exported in TypeScript.

import { Writable } from "stream";
export declare type Compressor = (source: string, dest: string) => string;
export declare type Generator = (time: number | Date, index?: number) => string;
export interface Options {
  compress?: boolean | string | Compressor;
  encoding?: BufferEncoding;
  history?: string;
  immutable?: boolean;
  initialRotation?: boolean;
  interval?: string;
  intervalBoundary?: boolean;
  maxFiles?: number;
  maxSize?: string;
  mode?: number;
  path?: string;
  rotate?: number;
  size?: string;
  teeToStdout?: boolean;
}
export declare class RotatingFileStream extends Writable {}
export declare function createStream(filename: string | Generator, options?: Options): RotatingFileStream;

Licence

MIT Licence

Bugs

Do not hesitate to report any bug or inconsistency @github.

ChangeLog

ChangeLog

Donating

If you find useful this package, please consider the opportunity to donate some satoshis to this bitcoin address: 12p1p5q7sK75tPyuesZmssiMYr4TKzpSCN