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

log-harvestor-node

v2.0.3

Published

The node module for Log Harvestor

Downloads

25

Readme

log-harvestor-node

Rate on Openbase

Documentation

See API Docs for Log-Harvestor.

This package is specific to NodeJS. Please see our docs for other supported languages, or use standard HTTP requests.

Installation


npm install log-harvestor-node

or

yarn add log-harvestor-node

Getting Started


This package requires that you have a Log Harvestor account, and Forwarder's created. If you have not done this yet:

  1. Go to LogHarvestor.com
  2. Register for a new Account (This is free) Register
  3. Create a new Forwarder - Link
  4. Generate a Forwarder Token

Now you can use this forwarder token to send logs, by adding it to a new Forwarder.

const { Forwarder } = require('log-harvestor-node');

const FWDR_TOKEN = 'your_forwarder_token'

const fwdr = new Forwarder({token: FWDR_TOKEN})
fwdr.log({ type: 'test', msg: { title: 'Hello World' } })

Configuration


| Option | Default | Description | | :--- | :----: | :--- | | BATCH | false | Batch mode sends logs in batches | | INTERVAL | 10 | Time between batches in seconds | | VERBOSE | false | Verbose mode prints info to the console |

Sending Logs


const { Forwarder } = require('log-harvestor-node');
const FWDR_TOKEN = 'your_forwarder_token'
const fwdr = new Forwarder({token: FWDR_TOKEN})

/* Log Types 
    - The log type is a string
    - This is the primary way logs are categorized & indexed
*/
fwdr.log({ type: 'any',     msg: 'message' })
fwdr.log({ type: 'thing',   msg: 'message' })
fwdr.log({ type: 'works',   msg: 'message' })

/* Log Messages 
    - Any valid type that you want
*/

// Numbers
fwdr.log({ type: 'test',    msg: 123456789 })
fwdr.log({ type: 'test',    msg: 0.000212  })
// Strings
fwdr.log({ type: 'test',    msg: 'What is my purpose?' })
fwdr.log({ type: 'test',    msg: 'You forward logs...' })
fwdr.log({ type: 'test',    msg: '-o_O-' })
// Arrays
fwdr.log({ type: 'test',    msg: [1,2,'3'] })
fwdr.log({ type: 'test',    msg: ['I', { logs: '<3' }, '!' ] })
// Objects
fwdr.log({ 
    type: 'test',    
    msg: { 
        title: 'Hello World', 
        desc: { 
            so: 'long', 
            and: 'thanks for all the fish!' 
        },
        trace: '42' 
    } 
})

Handling Errors


/* ASYNC */
const example = async () => {
    try{
        await res = fwdr.log({type: 'hello', msg: 'world'})
    }catch(e){
        // Handle Error Logic
    }
}

/* Then/Catch */
fwdr.log({type: 'hello', msg: 'world'})
    .then(() => {})
    .catch(() => {})

Connection Test


const { Forwarder } = require('log-harvestor-node');
const BAD_TOKEN = 'invalid_token'

const fwdr = new Forwarder({token: BAD_TOKEN})

/* 
    testConn returns a promise. 
    You can handle it however you like.
*/
fwdr.testConn()
    .then(() => {})
    .catch(() => {})

Config Validation


const { Forwarder } = require('log-harvestor-node');
const INVALID_TOKEN = 'invalid token'
const VALID_TOKEN = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6ImZvcndhcmRlciJ9.eyJfaWQiOiI2MTI4OTIwYjNjMzQyNTAwMjFkZGQyMTciLCJpYXQiOjE2MzAwNDg3ODN9.sb8lfpp01CC-y0T9Z5XiIEdy-JBeDHSBD8Gd05bZYaQ'

/* Valid Tokens are JWTs */

const validTest = Forwarder.validateConfig({token: INVALID_TOKEN})
console.log(validTest)
/* 
    { valid: true, errors: [] }
*/

const invalidTest = Forwarder.validateConfig({token: VALID_TOKEN})
console.log(invalidTest)
/* 
    {
        valid: false,
        errors: [
            ConfigValidtionError.INVALID_TOKEN
        ]
    }
*/

Multiple Forwarders


const { Forwarder } = require('log-harvestor-node');

const FWDR_TOKEN_ONE = 'your_forwarder_token_one'
const fwdrOne = new Forwarder({token: FWDR_TOKEN_ONE})
fwdrOne.log(...)


const FWDR_TOKEN_TWO = 'your_forwarder_token_two'
const fwdrTwo = new Forwarder({token: FWDR_TOKEN_TWO})
fwdrTwo.log(...)

Same Forwarder - Multiple Configs


const { Forwarder } = require('log-harvestor-node');

const FWDR_TOKEN = 'your_forwarder_token'

const fwdrMain = new Forwarder({token: FWDR_TOKEN})
const fwdrSecondary = new Forwarder({token: FWDR_TOKEN})

fwderMain.log({type: 'super', msg: 'flexible'})
fwderSecondary.log({type: 'json', msg: 'is awesome'})

Batching


This is one of the more complex functionalities of LogHarvestors SDK

Batch Mode, enables the forwarder to send logs on a polling-style interval

To enable batch just set { batch: true } when creating the forwarder

Now whenever you create a new log, it will be added to the bucket

The forwarder will check it's bucket on an interval, and send all the logs within the bucket in a single request - saving on bandwith.

To control the frequency of the batching, just set the interval { interval: {{ INTEGER }} }

For example:

const { Forwarder } = require('log-harvestor-node');
const FWDR_TOKEN = 'your_forwarder_token'

const fwdr = new Forwarder({
    token: FWDR_TOKEN,
    batch: true,
    interval: 60 // 60 Second interval
})

To test this, try implementing the snippet bellow:

const { Forwarder } = require('log-harvestor-node');
const FWDR_TOKEN = 'your_forwarder_token'

const fwdr = new Forwarder({
    token: FWDR_TOKEN,
    batch: true,
    interval: 30 // Every 30 seconds, your forwarder will try to send a batch of logs
})

fwdr.log({type: 'savin', msg: 'bandwidth'})
fwdr.log({type: 'batch', msg: 'mode rocks!'})

console.log(fwdr.bucket)
/* 
[
    { id: 12341591234, log: { type: 'savin', msg: 'bandwidth' } },
    { id: 76841587912, log: { type: 'batch', msg: 'mode rocks!' } },
]
*/
setTimeout(() => {
    console.log(fwdr.bucket)
/* No more logs!
    []
*/
}, 32000) // 32 Seconds

Recomendations


  1. Keep your Logging specific, and consise. This makes searching faster and more accurate
  2. No need to add timestamps or info about the forwarder. This information is automatically included with the log.