Socket
Book a DemoInstallSign in
Socket

logbuffer

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

latest
Source
npmnpm
Version
1.0.1
Version published
Maintainers
1
Created
Source

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
}]
*/

Keywords

data structure

FAQs

Package last updated on 07 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts