@spring-global/react-logger v0.0.12
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/react-logger
Usage for React
import React from "react"
import ReactDOM from 'react-dom'
import { SpringLogger, Integrations } from "@spring-global/react-logger"
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/react-logger"
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.info(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
By default SpringLogger exports the following function that allow us to write some logs manually:
- 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
- warning: Allows users to write warning logs, by default severity is TSeverity.WARNING
- error: Allows users to write warning logs, by default severity is TSeverity.FATAL
import { SpringLogger, TSeverity } from "@spring-global/react-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 and data is optional
SpringLogger.error( message, data ) // triggers a fatal error log and 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/react-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