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 🙏

© 2025 – Pkg Stats / Ryan Hefner

hbh-logrotator

v0.0.2

Published

Lightweight Node.js JSON log rotation library with flat and grouped logging support. Automatically rotates log files by entry count or time interval.

Readme

📦 hbh-logrotator

A lightweight, extensible JSON log rotation library for Node.js. Designed for high-volume logging with automatic file rotation, flat JSON logs, or grouped JSON logs.


✨ Features

  • 🔁 Automatic log rotation

    • Rotate by max number of entries
    • Rotate by time interval
  • 📄 Flat JSON logging

    • Logs stored as a JSON array
  • 🧩 Grouped JSON logging

    • Logs grouped by event type or custom key
  • ⚙️ Extensible base class

    • Create custom log formats easily
  • 🚀 Zero dependencies

  • 🧠 Efficient streaming using fs.createWriteStream


📥 Installation

npm install hbh-logrotator

📌 Exports

import { FlatJSON, GroupedJSON, Base } from 'hbh-logrotator';

| Export | Description | | ------------- | -------------------------------- | | FlatJSON | Writes logs as a flat JSON array | | GroupedJSON | Groups logs by an event key | | Base | Base class for custom rotators |


🛠️ Usage

1️⃣ Flat JSON Logger

Logs entries as a JSON array:

[
  { "event": "login", "user": "alice" },
  { "event": "logout", "user": "bob" }
]

Example

import { FlatJSON } from 'hbh-logrotator';

const logger = new FlatJSON('./logs', {
  maxEntries: 3
});

logger.write({ event: 'login', user: 'alice' });
logger.write({ event: 'logout', user: 'bob' });
logger.write({ event: 'login', user: 'charlie' });

➡️ Automatically rotates after 3 entries.


2️⃣ Grouped JSON Logger

Groups logs by event (or a custom key):

{
  "login": [
    { "event": "login", "user": "alice" }
  ],
  "logout": [
    { "event": "logout", "user": "bob" }
  ]
}

Example

import { GroupedJSON } from 'hbh-logrotator';

const logger = new GroupedJSON('./logs', {
  maxEntries: 5,
  eventKey: 'event'
});

logger.write({ event: 'login', user: 'alice' });
logger.write({ event: 'logout', user: 'bob' });

⚙️ Configuration Options

| Option | Type | Default | Description | | -------------------- | ---------------- | --------- | --------------------------------------- | | maxEntries | number | 1000 | Rotate file after this many log entries | | rotationIntervalMs | number \| null | null | Rotate file after time interval (ms) | | eventKey | string | 'event' | Key used for grouping logs |


🔁 Rotation Logic

A new log file is created when either condition is met:

  • currentEntries >= maxEntries
  • Date.now() - lastRotationTime >= rotationIntervalMs

Files are named automatically:

log_0.json
log_1.json
log_2.json
...

🧱 BaseLogRotator (Advanced Usage)

You can extend the base class to create custom log formats.

import { Base } from 'hbh-logrotator';

class CustomRotator extends Base {
  initFile() {
    this.currentStream.write('START\n');
  }

  handleEntry(entry) {
    this.currentStream.write(entry.message + '\n');
  }

  finalizeFile() {
    this.currentStream.write('END\n');
  }
}

🧹 Graceful Shutdown

Call flush() before exiting your app:

await logger.flush();

Ensures:

  • JSON is closed properly
  • File streams are safely ended

📂 Output Guarantees

| Logger | Output Format | Valid JSON | | ----------- | ------------- | ---------- | | FlatJSON | JSON Array | ✅ | | GroupedJSON | JSON Object | ✅ |


🧪 Use Cases

  • API request logging
  • Event tracking
  • Audit logs
  • Background jobs
  • Serverless environments
  • Data pipelines

📜 License

ISC License © HBH


  • Node.js log rotation
  • JSON logging
  • Log file rotation
  • Structured logging
  • Event-based logging
  • Lightweight logger
  • File-based logging