1.0.5 • Published 2 years ago
hv-logger v1.0.5
hv-logger
Health Vectors logging library.
Installation
Using npm
npm i hv-logger
Using yarn
yarn add hv-logger
Initialization
// ES5 Syntax
const HvLogger = require("hv-logger");
// ES6 Syntax
import HvLogger from "hv-logger";
const hvLogger = new HvLogger({
// Sentry enabled(true) or disabled(false)
sentry: true,
// Log data to console (true or false)
console: true,
// ignore error level logs for console
consoleIgnoreLevels: [],
// ignore error level logs for sentry
sentryIgnoreLevels: ["info"],
});
Usage
// Information log
hvLogger.info("This is an info", { foo: "bar" }, ["info_type", "log"]);
// Debug log
hvLogger.debug("This is a debug message", { foo: "bar" }, [
"debug_type",
"debug_log",
]);
// Warning log
hvLogger.warn("This is a Warning", { foo: "bar" }, ["warning_type", "warn"]);
// Error log
hvLogger.error("This is an error", { foo: "bar" }, [
"error_type",
"report_gen",
]);
Usage with Sentry
If sentry is set as true in the HvLogger constructor then initialize the sentry sdk in the root of the project (i.e. where the express app is initialized)
Example:
import * as Sentry from "@sentry/node";
import express from "express";
// or using CommonJS
// const Sentry = require('@sentry/node');
// const express = require('express');
const app = express();
Sentry.init({
dsn: "https://@sentry.com/8", // enter your project dsn
integrations: [
// enable HTTP calls tracing
new Sentry.Integrations.Http({
tracing: true,
}),
// enable Express.js middleware tracing
new Sentry.Integrations.Express({
app,
}),
],
// Performance Monitoring
tracesSampleRate: 1.0, // Capture 100% of the transactions, reduce in production!,
});
// Trace incoming requests
app.use(Sentry.Handlers.requestHandler());
app.use(Sentry.Handlers.tracingHandler());
// All your controllers should live here
app.get("/", function rootHandler(req, res) {
res.end("Hello world!");
});
// The error handler must be registered before any other error middleware and after all controllers
app.use(Sentry.Handlers.errorHandler());
// Optional fallthrough error handler
app.use(function onError(err, req, res, next) {
// The error id is attached to `res.sentry` to be returned
// and optionally displayed to the user for support.
res.statusCode = 500;
res.end(res.sentry + "\n");
});
app.listen(3000);