npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ceroy/react-exception-logger

v1.3.8

Published

An Exception Logger which is a wrapper over the Azure App Insights to provide an easy integration for react applications

Downloads

29

Readme

React Exception Logger


npm version

React Exception Logger, is a wrapper over the Application Insights SDK, which takes away the need to write boiler plate codes to use the SDK.

Installation

npm i @ceroy/react-exception-logger

Implementation

Setup

To setup the App Insight Instance for the Logger, we wrap the topmost component in the Component tree with the higher order component withExceptionLogger() It takes three parameters:

  • Component : React.ComponentType<P>
  • connectionString : string
  • configurations? : IConfig
Example
import { withExceptionLogger, IConfig } from  '@ceroy/react-exception-logger'

const connectionString:string = "<YOUR-AZURE-APP-INSIGHTS-CONNECTION-STRING>"
const configurations:IConfig = {}

class HelloWorld extends React.Component<IHelloWorldProps, {}> {

  public render(): React.ReactElement<IHelloWorldProps> {
    return (
      <div>
        <h1> Hello World </h1>
      </div>
    );
  }
}

export default withExceptionLogger(HelloWorld,connectionString,configurations)

Note: The connectionString is available in your Azure App Insights Dashboard

Note: Although configurations is an optional parameter, but we strongly recommend to supply your custom configurations to automatically track telemetry which you are allowed for or is required

Usage

  1. Automatic: Thanks to the underlying Application Insights SDK, all the telemetry are automatically tracked taking in account any configurations if provided. Please go through the Configurations for more details.
  2. Manual: Manually as the name of the package suggests we can only log exceptions to the Azure App Insights using an exposed logException function.

Note: Any uncaught exceptions that occur before the instance of the logger is created are put in a queue, and once the instance is created all those queue exceptions are logged into the Azure App Insights

Manual Usage

logException()

This is the function that is used to manually log any caught exceptions to the analytics tool. Its of type LogExceptionFunctionType which takes 3 parameters:

  1. error: Error
  2. severityLevel?: SeverityLevel
  3. properties?: any

and returns void

Note: Although severityLevel is an optional parameter, and by default it is SeverityLevel.Information enum value, but we strongly recommed to supply the severity of the case, so as to filter the same while checking out the logs in the analytics tool based on severity.

There are three ways to get hold of the logException function

  1. Using props:

Whichever component we wrap with our withExceptionLogger high order component is supplied with an additional prop logExceptions

Example:

import { withExceptionLogger, IConfig, IWithExceptionLogger } from  '@ceroy/react-exception-logger'

const connectionString:string = "<YOUR-AZURE-APP-INSIGHTS-CONNECTION-STRING>"
const configurations:IConfig = {}

class HelloWorld extends React.Component<IHelloWorldProps & IWithExceptionLogger, {}> {
  public someFunction(){
	  try{
	  //Your Code
	  }catch(err){
		  this.props.logException(new Error(err))
	  }
  }
  public render(): React.ReactElement<IHelloWorldProps> {
    return (
      <div>
        <h1> Hello World </h1>
      </div>
    );
  }
}

export default withExceptionLogger(HelloWorld,connectionString,configurations)

Note: IWithExceptionLogger interface is only required for TypeScript projects, for javascript the function is directly available with this.props.logException() or props.logException() for Functional Components

  1. Using ExceptionLoggerConsumer:

The logException() is also passed as a Context for all the childrens. And hence can be used using the Consumer for the Context ExceptionLoggerConsumer. This can be used for any Class Based Components.

  1. Using useExceptionLogger() hook:

For Functional Component a hook useExceptionLogger() is avalaible. The useExceptionLogger() returns a default function of type LogExceptionFunctionType

Example (For any Child Component):

import { useExceptionLogger } from  '@ceroy/react-exception-logger'

function  Child() {
   const  logger = useExceptionLogger()
   return (

   	<div>
   		<PrimaryButton  text="Click to Log an Exception"  onClick={() => { logger(new  Error('Button Clicked'),) }}  />
   	</div>
   )
}
export  default  Child

Future Releases

  1. Improving the Documentation
  2. Making the package support other analytics tools like Google analytics, etc down the line.