Rollbar is a real-time cloud error logging service that has SDKs for a wide variety of languages, frameworks, and platforms.
Rollbar provides in-depth trace logs for any uncaught exception in your application code, along with allowing different level log entries to be called manually. It makes tracing back the path your users take to create errors a much simpler task.
The extensive configuration options also allow different environment and version tags, as well as user attribution to assist further.
Included in the free tier Rollbar provides up to 5000 events per month and real-time alerts. You'll need a free account to be able to create a project and add the configuration to your Ionic app.
To add Rollbar into your Ionic Angular app:
- In a terminal, open up your Ionic project directory
- Run npm i rollbar
- Run ng generate service services/rollbar to create a new service for your app.
In the rollbar.service.ts file that is generated, add the following imports & consts:
The type of Rollbar is an interface, which means we can't inject it into our app in standard ways. In order to use it properly, we need to use an InjectionToken instead. This allows us to assign the value of Rollbar to a token that can be injected into our components and services.
In addition to creating this service, it must be included in our app.module.ts:
Breaking the service down, we have the class implementing the Angular ErrorHandler. This means that the RollbarErrorHanlder class must implement a function called handleError(err: any):
export class RollbarErrorHandler implements ErrorHandler
This function is the one that Ionic now uses by default, overloading the in-built one, to handle any uncaught exception that occurs and automatically logs the error to Rollbar. In this case, we are simply logging to Rollbar an "error" level log. This could be customised to manipulate the error and/or log different levels depending on the error type.
In our class constructor, we are using the Device plugin provided by Capacitor to retrieve our app's version number. This is purely added to the Rollbar configuration object to add information to the error logs that are sent.
Following this, we have addRollbarPerson(user). In our apps, this function is called after the user logs in. The user argument contains the information retrieved from the database about the user. This allows the logs to be attributed to a user.
Lastly, we export Rollbar into a factory which we then use as a provider in our app.module.ts
If you liked this post, please share it!