Giter Club home page Giter Club logo

5log-sdk's Introduction

5Log Software Development Kit - Client

We ๐Ÿซถ NodeJS

This is the official NodeJS SDK for 5log, a cloud based logging and bug tracking app

Installation

๐Ÿ‘‰ Using NPM

# install locally (recommended)
npm i --save 5log-sdk

๐Ÿ‘‰ Usage

// ES6
import { filog } from '5log-sdk'

// init
const log = new filog(
    {
        source: {
            app_name: 'your-app-name',
            app_version: '1.0.0'
        },
        environment: 'development',
        transports: [
            { 
                auth: { 
                    name: 'x-client-id', 
                    type: 'ApiKey', 
                    value: 'fg_jlad84djkadfnjc84=' 
                }, 
                url: 'https://logs.devops.io/api/v1/logs', 
                logType: 'ANY' 
            }
            // if you want to separate log into different api service you can add more options
            { 
                auth: { 
                    name: 'x-client-id', 
                    type: 'ApiKey', 
                    value: 'fg_379asdajsnd84hdaf=' 
                }, 
                url: 'https://error.devops.io/api/v1/logs', 
                logType: 'ERROR' 
            }
        ]
    }
)

// Test Scenario
function JsonParse (value) {
    try {
        return JSON.parse(value)
    } catch (error) {
        log.write({
            logLevel: 'ERROR',
            source: {
                app_name: '{{your-app-name}}',
                package_name: '{{your-package-name}}',
                app_version: '{{app-version}}',
            },
            errorDescription: error,
            environment: '{{your-environment}}',
            eventCode: 'ERR-2180'
        }, { 
            verbose: 'true', 
            originalError: error
        })
    }
}

JsonParse("{a;b}");

Other Method

const testError = () => {
    try {
        throw new Error('Error raised')
    } catch (error) {
        // accept 2 argument ( error, eventCode )
        // you can use error.name as your eventCode or generate a custom eventCode
        log.error(error, { eventCode: error.name })
        // with custom code
        log.error(error, { eventCode: 'E-007' })
    }
}

Available Log Type

For now, we only provide logging for error, warning, debug, and info types and we may include custom log types in the future.

This logger accepts the following parameters:

name value type description
error Error
options - Object
eventCode String Default: error name like SyntaxError, ReferenceError or you can create your own custom eventCode
printOut Boolean If you set True, the error message will show up in your console/terminal. Set False if you use the throw new Error method on catching errors.
payload Object If you have your own logger API, you can create a custom schema based on your API requirements.

Example on using printOut

function trapError () {
    try {
        // your code
    } catch (error) {
        log.error(error, { printOut: false })
        // let the CustomError print out the error message
        throw new CustomError(`This error is suck`)
    }
}

Example using custom schema on payload

Say your backend has extra requirements such as logId, functionName, and timestamp. Then, set the payload as shown in the example below.

function trapError () {
    try {
        // your code
    } catch(error) {
        log.error(error, {
            payload: {
                // your API requirement
                logId: Crypto.uuid(),
                functionName: 'postIt',
                timestamp: Date.now()
            }
        })
    }
}

Note

Log levels such as ERROR, WARNING, DEBUG, or INFO are automatically provided by filog, and filog will generate the error details for you

Handling Uncaught Exception & Unhandled Rejection

// ES6
import { filog } from '5log-sdk'

// init
const log = new filog(
    {
        source: {
            app_name: 'your-app-name',
            app_version: '1.0.0'
        },
        environment: 'development',
        transports: [
            { auth: { name: 'x-client-id', type: 'ApiKey', value: 'fg_jlad84djkadfnjc84=' }, url: 'https://logs.devops.io/api/v1/logs', logType: 'ANY' }
            // if you want to separate log into different api service you can add more options
            { auth: { name: 'x-client-id', type: 'ApiKey', value: 'fg_379asdajsnd84hdaf=' }, url: 'https://error.devops.io/api/v1/logs', logType: 'ERROR' }
        ]
    }
)

// Start by listening for any errors that might occur.
log.errorListener()

// your code goes here

Filog Transport Method

Currently, Filog only supports the HTTP transport method and RabbitMQ as a message broker. In the future, it will also support the use of Apache Kafka.

Example of using RabbitMQ:

import { filog } from '5log-sdk';

const logger = new filog({
    transports: [
        { 
            auth: { 
                name: 'x-client-id', 
                type: 'ApiKey', 
                value: 'fg_379asdajsnd84hdaf=' 
            }, 
            url: "amqp://username:password@host:5672/vhost?heartbeat=5&connection_timout=1000#exchange-name:queue-name:routekey", 
            logType: "any"
        }
    ]
})

// set payload wrapper name if you like (optional)
logger.setMessageWrapper("MyErrorPayload");
// example if you have more requirement on message properties / object (optional)
logger.addMessageProperties({
    task: 'create',
    messageId: 'tx-9000',
})

// rest of your code

RabbitMQ service will see your payload as :

{
    "MyErrorPayload": {
        // your payloads
    },
    // your additional wrapper
    "task": "create",
    "messageId": "tx-9000"
}

If you don't specify any, it will look like this :

{
    "payload": {
        // your payloads
    },
    // your additional wrapper
    "task": "create",
    "messageId": "tx-9000"
}

Note

To specify an exchange name, queue name, and routing key in the URL parameter, use a colon as a separator.

RabbitMQ Options

Filog uses the default options for RabbitMQ configuration, such as the exchange type, queue type or aguments. However, you can customize these settings based on your requirements.

Parameter Value Default
Exchange Type direct, fanout, headers, topics direct
Exchange Argument alternate-exchange Not Set
Queue Type classic, quorum, stream classic
Queue Arguments x-dead-letter-exchange exchange name
x-dead-letter-routing-key Not Set
x-single-active-consumer Not Set
x-expires Not Set
x-message-ttl Not Set
x-overflow valid value: drop-head, reject-publish or reject-publish-dlx Not Set
x-max-length Not Set
x-max-length-bytes Not Set
x-queue-leader-locator Not Set

Example:

import { filog } from '5log-sdk';

const logger = new filog({
    transports: [
        { 
            auth: { 
                name: 'x-client-id', 
                type: 'ApiKey', 
                value: 'fg_379asdajsnd84hdaf=' 
            }, 
            url: "amqp://username:password@host:5672/vhost?heartbeat=5&connection_timout=1000#exchange-name:queue-name:routekey", 
            logType: "any"
        }
    ]
})

logger.setPublisherOpts({
    exchangeType: 'fanout',
    exchangeArgument: { 'alternate-exchange': 'my-second-exchange' },
    queueArguments: {
        'x-queue-type': 'quorum',
        'x-dead-letter-exchange': 'my-exchange'
        // and so on
    }
})

// rest of your code

GraphQL

Below is an example of how to configure Filog to send logs to your GraphQL service.

import { filog } from '5log-sdk';
// initiate new filog
const logger = new filog({
    transports: [
        {
            auth: { 
                name: 'x-client-id', 
                type: 'ApiKey', 
                value: 'fg_379asdajsnd84hdaf=' 
            },
            url: 'http(s)://<hostname>/gql',
            logtype: 'any'
        }
    ]
})

// Mutation Query. We wrapped our error payload with PAYLOAD
const withWrapper = `
    mutation ($payload: MutationInputType) {
        storingLogs(payload: $payload) {
            logLevel,
            errorDescription
        }
    }
`
// Mutation query without using any wrapper
const withoutWrapper = `
    mutation($logLevel: String!, $errorDescription: String!, $timeStamp: Date!) {
        storingLogs(logLevel: $logLevel, errorDescription: $errorDescription, timeStamp: $timeStamp) {
            logLevel,
            errorDescription
        }
    }
`
// setGraphQLQuery accept two arguments, query and variableWrapper
logger.setGraphQLQuery(withWrapper, 'payload')
// if you dont use any wrapper, just leave it out
logger.setGraphQLQuery(withoutWrapper)

// test scenario
function trapMyError() {
    try {
        throw new Error(`This is a trap!`)
    } catch(error) {
        logger.error(error, {
            // set your graphQL variables
            variables: {
                timeStamp: Date.now()
                // you're only need specify one variable here 
                // because filog will compile it alongside logLevel 
                // and errorDescription as a default variable
            }
        })
    }
}

Note

Filog will send default data / variable such as logLevel, errorDescription, source and environment. The last two variable will exist if you set it on filog init

In Project Example

Please refer to this Example for more usage in real projects

5log-sdk's People

Contributors

permaficus avatar genzkids avatar

Stargazers

Rezha Bahari avatar  avatar  avatar

Watchers

 avatar

Forkers

genzkids

5log-sdk's Issues

Typo on RBMQ Library when initiate exchange

Bug Description

[RBMQ] Error: Connection closed: 503 (COMMAND-INVALID) with message "COMMAND_INVALID - unknown exchange type 'topics

Expected Behavior

None

Version

v0.6.40

Transports is missing BrokerExchangeInterface

Bug Description

Type '{ client_id: string; url: string; logType: "ANY"; }' is not assignable to type 'FilogInitObject'.
  Type '{ client_id: string; url: string; logType: "ANY"; }' is missing the following properties from type 'BrokerExchangeInterface': channel, name, type, durable(2322)

Expected Behavior

Without assigning any config or options for message broker at transports should be just fine

How To Reproduce

Import and create new filog

Version

Latest (v0.6.8)

TypeError: Cannot convert undefined or null to object

Error Description

/5log-SDK/dist/libs/publisher.js:42
        if (Object.entries(extraWrapper).length !== 0) {
                   ^

TypeError: Cannot convert undefined or null to object
    at Function.entries (<anonymous>)
    at /Users/permaficus/Documents/Projects/5log-SDK/dist/libs/publisher.js:42:20
    at Generator.next (<anonymous>)
    at fulfilled (/Users/permaficus/Documents/Projects/5log-SDK/dist/libs/publisher.js:5:58)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Expected Behavior

How To Reproduce

Just using try/catch to reproduce

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.