Write to a file and rotate it when a counter reaches a limit
npm install logrotate-by-counterwrite``bash`
$ npm install logrotate-by-counter
`js
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}.${date}.rotated and the current file being written to${prefix}.${pid}.${epoch}.0
before rotation is of the form . The epoch is an increasing counter
that is incremented every time we rotate to prevent rotation collisons.
If you want to rotate anything already inflight then create a new instance and call stop onStoppedError
the previous instance. Writes on a stopped instance generate error events of the form which{msg: 'Can not write to a stopped Rotator instance', line: string}`.
is an object that looks like