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

logrotate-by-counter

v1.0.7

Published

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

Downloads

11

Readme

What is it?

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.

Why is it?

This came up when I wanted to do batch processing using a fixed number of records/events and needed a way to write a fixed number of things to a log for later processing. I looked around and didn't see anythig that fit the bill so here it is.

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}.${date}.rotated 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.

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}.