0.0.11 • Published 3 years ago

@spring-global/spring-logger-node v0.0.11

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

Spring Logger

This is a package for capturing error messages and tracking their origin, we include at first Sentry as a third party company for logging purposes but it can support others APIs.

Installation

npm i @spring-global/spring-logger-node

Usage for node

import { SpringLogger, Integrations, TSeverity } from "@spring-global/spring-logger-node"
// SpringLogger initialization with Sentry node API integration
SpringLogger.init( new Integrations.NodeSentryLogger("http:<url external API>"))

try{
  throw new Error("My first error!")
}catch( e ){
  // The package exposes the verbose level in the variable TSeverity
  SpringLogger.log(e, {id: 123, name: "Peter"}, TSeverity.INFO )
}

Usage for node with Express

The integration with express allows the logger to record the endpoint name that was called.

import express, { IRouter } from "express"
import { SpringLogger, Integrations, TSeverity } from "@spring-global/spring-logger-node"
import userRouter from "./routes/userRouter"

const app = express()
const router: IRouter = app.Router()
// SpringLogger initialization with Sentry node with express API integration
SpringLogger.init( new Integrations.NodeSentryLogger("http:<url external API>"), app )

// Unhandle error should be tracked by SpringLogger
app.get("/unhandle_error", function mainHandler(req, res) {
  throw new Error("My first error!")
})

// Manual trigger error tracking
app.get("/trigger_error", function manualError(req, res) {
  try{
    throw new Error("My first error!")
  }catch( e ){
    // The package exposes the verbose level in the variable TSeverity
    SpringLogger.log(e, {id: 123, name: "Peter"}, TSeverity.INFO )
  }
})

router.use("/user", userRouter)

// End SpringLogger initialization
SpringLogger.end( app )

app.listen( 8080 , function () {
  console.log( "API is running on port 8080")
})
// ./routes/userRouter.js
import { SpringLogger } from "@spring-global/spring-logger-node"
import { Router } from 'express';

const userRouter = Router();

userRouter.get('/', (req, res) => {
  try{
    throw new Error("My first error from users router!")
  }catch( e ){
    // trigger manually a warning log from user router
    SpringLogger.warning(e)
  }
  return res.json("success");
})

export default userRouter

Methods for recording logs manually

SpringLogger exports the following functions that allow us to write logs manually:

  1. log: Allows users to write any kind of log by passing the severity parameter: TSeverity.DEBUG, TSeverity.INFO, TSeverity.WARNING, TSeverity.ERROR, TSeverity.FATAL. By default severity is TSeverity.INFO
  2. info: Allows users to write info logs, by default severity is TSeverity.INFO
  3. debug: Allows users to write debug logs, by default severity is TSeverity.DEBUG
  4. warning: Allows users to write warning logs, by default severity is TSeverity.WARNING
  5. error: Allows users to write error logs, by default severity is TSeverity.ERROR
import { SpringLogger, TSeverity } from "@spring-global/spring-logger-node"
/**
 * Some code here
 * */
SpringLogger.log( message, data, severity ) // data is optional and severity can take one of the values: TSeverity.DEBUG, TSeverity.INFO, TSeverity.WARNING, TSeverity.ERROR, TSeverity.FATAL
SpringLogger.info( message, data ) // triggers a info log, data is optional
SpringLogger.debug( message, data ) // triggers a debug log, data is optional
SpringLogger.warning( message, data ) // triggers a warnig log, data is optional
SpringLogger.error( message, data ) // triggers a error log, data is optional

Note:

Before using these methods you must inicialize the API by calling the method init.

Recording execution time

This tool also allows to record the execution time of any process in your application, the code below shows how to implement it manually:

import { SpringLogger } from "@spring-global/spring-logger-node"
/**
 * Some code here
 * */

// Defines the starting timestamp
SpringLogger.start("Process one");

/**
 * Some process code here
 * */

// Defines the end timestamp
const executionTime = SpringLogger.finish("Process one"); // It returns the execution time

Disabling tracking logs

By default all logs are going to be tracked and showed in the console by the API unless the user sets the variable SPRING_LOGGER_ENABLED as false in the .env file

0.0.11

3 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago