JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q39597F
  • License MIT

Write to a file and rotate it when a counter reaches a limit

Package Exports

  • logrotate-by-counter

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (logrotate-by-counter) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

What is this?

It is a log writer/rotator that rotates logs based on a counter instead of log file size or timestamp. The counter tracks number of calls to write so if you want to rotate based on number of lines then this is a good approximation.

How do I use it?

$ npm install logrotate-by-counter
import { Rotator } from 'logrotate-by-counter';

const rotator = new Rotator('1-1', __dirname, 10);
rotator.events.on('error', (error) => {
    console.error('Received error event from rotator: ', error);
});

for (let i = 0; i < 20; i++) {
    rotator.write(`${i}\n`);
}

First argument is a unique prefix to identify the log files generated by this instance of the rotator. The second argument is where you want the log files to appear. The third argument is the counter that tracks the number of write calls and rotates the file when the limit is reached.

The rotated files are of the form ${prefix}.${pid}.${epoch}.0.${date} and the current file being written to before rotation is of the form ${prefix}.${pid}.${epoch}.0. The epoch is an increasing counter that is incremented every time we rotate to prevent rotation collisons. The way the rotation is done there will be an extra empty line at the end of each rotated file. Appending a newline forces a flush on any outstanding writes so that's why there is an extra line at the end of the rotated file.

If you want to rotate anything already inflight then create a new instance and call stop on the previous instance. Writes on a stopped instance generate error events of the form StoppedError which is an object that looks like {msg: 'Can not write to a stopped Rotator instance', line: string}.