NestJS Rollbar package allows you to extend standard Logger to send errors directly to Rollbar. Usage if quite simple. Get Rollbar Token from your project page and install nestjs-rollbar package:
npm i report-to-rollbar nestjs-rollbar
Then register module in your root app.module
import { LoggerModule } from 'nestjs-rollbar';
import { ReportRollbarModule } from 'report-to-rollbar'
@Module({
imports: [
// ...
LoggerModule.forRoot({
accessToken: process.env.ROLLBAR_ACCESS_TOKEN,
environment: process.env.ROLLBAR_ENVIRONMENT
}),
ReportRollbarModule,
],
})
export class AppModule {}
To use RollbarLogger in any service or controller just inject it in constructor:
import {
ReportRollbarService,
Type
} from 'src/report-rollbar/report-rollbar.service'
constructor(
private readonly reportRollbarService: ReportRollbarService
) {}
To log an error to Rollbar just call reportToRollbar()
method:
try {
throw new Error('Test error');
} catch (error) {
this.rollbarLogger.reportToRollbar(
`myMethod`,
e,
Type.error,
null,
e.data,
e.config,
null
)
}
First parameter is the name of your method to show in logs, second an error object third the Type of the error, fourth the payload, fifth the variables, sixth the request sent, and seventh the environment chosen.
See Changelog for more information.
Contributions welcome! See Contributing.
Keybe - Mauro Cominotti
Licensed under the MIT License - see the LICENSE file for details.