Skip to main content

Node.js

Prerequisites

Assuming you are completed getting started guide:

Install SDK

You can download an official Node.js sdk of Loghammer by using this command.

npm install @loghammer/nodejs-sdk

Initialize the SDK

To start, we need to initialize the SDK. To initialize the SDK, you need to call initLoghammer() function at your projects entry point with your crendentials.

import { initLoghammer } from "@loghammer/nodejs-sdk"

initLoghammer({
clientId: "your-client-id",
clientSecret: "your-client-secret"
})

Automatically capture errors

By calling registerGlobalErrorHandling() function at the entry point of your project, Loghammer SDK will capture all of the errors automatically.

registerGlobalErrorHandling() function takes one argument which is string array. This parameter is array of custom tags when global error handler captures errors and reports it with the provided tags.

import { initLoghammer, registerGlobalErrorHandling } from "@loghammer/nodejs-sdk"

initLoghammer({
clientId: "your-client-id",
clientSecret: "your-client-secret"
})

registerGlobalErrorHandling(["global-error-tag", "example-tag"])

Create info log

To create info log, you need to call createInfoLog() function. This function accepts one argument which is an LoghammerInfoLogProps.

import { createInfoLog } from "@loghammer/nodejs-sdk"

await createInfoLog({
message: "This is an info log.",
data: {
"mock": "data"
}
})

Create error log

To create error log, you need to call createErrorLog() function. This function accepts one argument which is an LoghammerErrorLogProps.

import { createErrorLog } from "@loghammer/nodejs-sdk"

await createErrorLog({
message: "This is an error log.",
stackTrace: new Error("mock error").stack,
env: {
os: "linux"
},
tags: ["payment-provider"]
})

Create track log

To create track log, you need to call createTrackLog() function. This function accepts one argument which is an LoghammerTrackLogProps.

import { createTrackLog } from "@loghammer/nodejs-sdk"

await createTrackLog({
message: "This is a track log.",
data: {
"user": "[email protected]"
// or id stuff you decide what to log 🤪
}
tags: ["visit-pricing-page"]
})