Coder Social home page Coder Social logo

log-harvestor-node's Introduction

LogHarvestor Logo

Log Harvestor - LinkedIn   LogHarvestor - Twitter   Log Harvestor - You Tube

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.

LogHarvestor Logo

log-harvestor-node's People

Contributors

solomon-bush avatar

Stargazers

Wouter Haringsma avatar Ivan A. avatar  avatar

Watchers

 avatar

log-harvestor-node's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.