0.0.8 • Published 3 years ago

@spring-global/node-logger v0.0.8

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/node-logger

Usage for node with Express

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

const app = express()
const router: IRouter = app.Router()
// SpringLogger initialization with Sentry node 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.info(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/node-logger"
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

By default SpringLogger exports the following function that allow us to write some logs manually:

  1. info: 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. warning: Allows users to write warning logs, by default severity is TSeverity.WARNING
  3. error: Allows users to write warning logs, by default severity is TSeverity.FATAL
import { SpringLogger, TSeverity } from "@spring-global/node-logger"
/**
 * Some code here
 * */
SpringLogger.info( 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.warning( message, data ) // triggers a warnig log, data is optional
SpringLogger.error( message, data ) // triggers a fatal error log, data is optional

Note:

Before using this method you should have executed the init method first, whether in this module or in some parent module.

Recording execution time

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

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

// Defines the starting timestamp
const startTimestamp = SpringLogger.start( "Process one" ); // It returns the start timestamp

/**
 * Some process code here
 * */

// Defines the end timestamp
const endTimestamp = SpringLogger.finish(); // It returns the end timestamp

Disabling tracking logs

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

Debugging with console

By default debug option is disabled but users can set the variable SPRING_LOGGER_DEBUG as true in the .env file for enabling console outputs

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

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