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

logbuffer

v1.0.1

Published

Specialized circular buffer for Node.js high performance logging: It buffers log entries in memory and flushes the data to a callback function later. Triggered by size threshold or time interval.

Downloads

13

Readme

logbuffer

Specialized circular buffer for Node.js high performance logging: It buffers log entries in memory and flushes the data to a callback function later. Triggered by size threshold or time interval.

Installation

npm install logbuffer

Usage

Example:

const logBuffer = require('logbuffer');

// Set capacity to 100 entries
const capacity = 100;

// Set autoflush (optional)
const autoflush = { 
    enabled: true, 
    callback: (data) => {
        console.log( `autoflush'd: ${ JSON.stringify(data, null, 4) }` );
    },
    interval: 15000
}

// Create logBuffer object
let lb = new logBuffer(capacity, { autoflush: autoflush });

// Add log entry
lb.add( {message: 'Logged!', ts: new Date().getTime()} );

setTimeout( () => {
    // Add another log entry
    lb.add( {message: 'Logged again!', ts: new Date().getTime()} );
}, 2500);

How autoflush works

If autoflush is enabled, the callback function is triggered as soon as the buffer is filled up. The data is flushed to the callback function, the buffer starts empty again.

If an interval is set and the capacity is not reached within the time limit, the same as described above happens and the interval is restarted.

For our "Example" that means: flush all data to the callback function when the buffer holds 100 entries or if 15 seconds are over - whatever occurs first.

API Methods

capacity()

Returns the overall capacity of the buffer.

Example:

console.log( lb.capacity() );
// 100

size()

Returns the current filling level of the buffer.

Example:

console.log( lb.size() );
// 2

add(entry)

Adds a log entry to the buffer.
You can add strings and/or objects - but don't forget to handle them correctly when you get the data back!

Example:

let res = lb.add('This is a log line.');

console.log( res );
// OK

flush(callback)

Flushes current data and empties the buffer.

Example:

let data = lb.flush();

console.log( JSON.stringify(data, null, 4) );
/*
[{
    "message": "Logged!",
    "ts": 1473177268820
}, {
    "message": "Logged again!",
    "ts": 1473177271335
}]
*/