Skip to content

Commit

Permalink
chore: Implement custom logger logic for errors (#632)
Browse files Browse the repository at this point in the history
Logger.error will now send the error message to sentry
  • Loading branch information
sashko9807 authored May 15, 2024
1 parent 5b90288 commit 4351261
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
3 changes: 3 additions & 0 deletions apps/api/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ import { MarketingNotificationsModule } from '../notifications/notifications.mod
import { StatisticsModule } from '../statistics/statistics.module'
import { AffiliateModule } from '../affiliate/affiliate.module'

import { LoggerModule } from '../logger/logger.module'

@Module({
imports: [
ConfigModule.forRoot({ validationSchema, isGlobal: true, load: [configuration] }),
Expand Down Expand Up @@ -123,6 +125,7 @@ import { AffiliateModule } from '../affiliate/affiliate.module'
CampaignNewsModule,
CampaignNewsFileModule,
MarketingNotificationsModule,
LoggerModule,
],
controllers: [AppController],
providers: [
Expand Down
8 changes: 8 additions & 0 deletions apps/api/src/logger/logger.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common'
import { MyLogger } from './logger'

@Module({
providers: [MyLogger],
exports: [MyLogger],
})
export class LoggerModule {}
16 changes: 16 additions & 0 deletions apps/api/src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ConsoleLogger, Injectable } from '@nestjs/common'
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry'

@Injectable()
export class MyLogger extends ConsoleLogger {
constructor(@InjectSentry() private readonly client: SentryService) {
super()
}

error(message: any, stack?: string, context?: string) {
// add your tailored logic here

this.client.instance().captureMessage(message, 'error')
super.error(message, stack, context)
}
}
3 changes: 3 additions & 0 deletions apps/api/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { setupSwagger } from './config/swagger.config'
import { setupExceptions } from './config/exceptions.config'
import { setupValidation } from './config/validation.config'
import { setupShutdownHooks } from './config/shutdown.config'
import { MyLogger } from './logger/logger'
import { LoggerModule } from './logger/logger.module'

const globalPrefix = process.env.GLOBAL_PREFIX ?? 'api/v1'
const logLevels: LogLevel[] = ['error', 'warn']
Expand All @@ -26,6 +28,7 @@ async function bootstrap() {

app.setGlobalPrefix(globalPrefix)
app.enableVersioning({ type: VersioningType.URI })
app.useLogger(app.get(MyLogger))

const appVersion = process.env.APP_VERSION || 'unknown'
setupHelmet(app)
Expand Down
3 changes: 1 addition & 2 deletions apps/api/src/tasks/bank-import/import-transactions.task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,7 @@ export class IrisTasks {
// No transactions for the day yet
if (!transactions.length) return
} catch (e) {
Logger.error(e.message)
throw new BadRequestException('Failed to get transactions data from Iris' + e.message)
return Logger.error('Failed to get transactions data from Iris' + e.message)
}

// 3. Prepare the BankTransaction Records
Expand Down

0 comments on commit 4351261

Please sign in to comment.