Rollbar, todas las llamadas regulares de console.log se muestran como originadas en rollbar.min.js:2251:82. Sin embargo, me gustaría poder ver de dónde provino el registro, por ejemplo, de header.ts:25, esto puede ser útil en algunos casos.
aplicación.módulo.ts
import { BrowserModule } from '@angular/platform-browser'; import { NgModule, ErrorHandler } from '@angular/core'; import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { RollbarService, rollbarFactory, RollbarErrorHandler } from './rollbar'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, AppRoutingModule ], providers: [ { provide: ErrorHandler, useClass: RollbarErrorHandler }, { provide: RollbarService, useFactory: rollbarFactory } ], bootstrap: [AppComponent] }) export class AppModule {}rollbar.ts
import * as Rollbar from 'rollbar'; import { Injectable, Inject, InjectionToken, ErrorHandler } from '@angular/core'; const rollbarConfig = { accessToken: 'CLIENT_ITEM_TOKEN', captureUncaught: true, captureUnhandledRejections: true, autoInstrument: { log: false }, }; export const RollbarService = new InjectionToken<Rollbar>('rollbar'); @Injectable() export class RollbarErrorHandler implements ErrorHandler { constructor(@Inject(RollbarService) private rollbar: Rollbar) {} handleError(err:any) : void { this.rollbar.error(err.originalError || err); } } export function rollbarFactory() { return new Rollbar(rollbarConfig); }¿Cómo habilitar solo el registro de errores si está en un entorno de producción? El uso de la clase angular ErrorHandler en un entorno local parece resolver mi problema. ¿Cómo puedo hacer esto?
Ejemplo:
if (!environment.production) { return new ErrorHandler() }