0.0.11 • Published 3 years ago

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

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

Spring React 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 install @spring-global/spring-logger-react

Usage for React

import React from "react"
import ReactDOM from 'react-dom'
import { SpringLogger, Integrations } from "@spring-global/spring-logger-react"
import App from './App';

// SpringLogger initialization
SpringLogger.init( new Integrations.ReactSentryLogger("http:<url external API>") )

ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  document.getElementById('root')
);
import React from "react"
import { SpringLogger, TSeverity } from "@spring-global/spring-logger-react"

const App = () => {
    const errorFunction = () => {
        throw new Error("This is my first Error!") // This error will be captured by default
    }

    const handleClick = () => {
        try{
            throw new Error("This is my first info Error!")
        }catch(e){
            SpringLogger.log(e, {id: 123, name: "Peter"}, TSeverity.INFO) // This is an example of triggering an error manually
        }
    }

    return (
        <button type="button" className="btnErrorClass" onClick={ errorFunction }>Break the world</button>
        <button type="button" className="btnInfoClass" onClick={ handleClick }>Trigger an info error</button>
    )
}

export default App;

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-react"
/**
 * 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 initialize 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-react"
/**
 * Some code here
 * */

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

/**
 * Some process code here
 * */

// Defines the end timestamp
const executionTime = SpringLogger.finish("Process one"); // 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

Enable console outputs

By default the logs will not be shown in the console, if you want to see the errors in the console please set the variable SPRING_LOGGER_CONSOLE as true 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.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago