Skip to content

Commit 4351261

Browse files
authored
chore: Implement custom logger logic for errors (#632)
Logger.error will now send the error message to sentry
1 parent 5b90288 commit 4351261

File tree

5 files changed

+31
-2
lines changed

5 files changed

+31
-2
lines changed

apps/api/src/app/app.module.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ import { MarketingNotificationsModule } from '../notifications/notifications.mod
6060
import { StatisticsModule } from '../statistics/statistics.module'
6161
import { AffiliateModule } from '../affiliate/affiliate.module'
6262

63+
import { LoggerModule } from '../logger/logger.module'
64+
6365
@Module({
6466
imports: [
6567
ConfigModule.forRoot({ validationSchema, isGlobal: true, load: [configuration] }),
@@ -123,6 +125,7 @@ import { AffiliateModule } from '../affiliate/affiliate.module'
123125
CampaignNewsModule,
124126
CampaignNewsFileModule,
125127
MarketingNotificationsModule,
128+
LoggerModule,
126129
],
127130
controllers: [AppController],
128131
providers: [

apps/api/src/logger/logger.module.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Module } from '@nestjs/common'
2+
import { MyLogger } from './logger'
3+
4+
@Module({
5+
providers: [MyLogger],
6+
exports: [MyLogger],
7+
})
8+
export class LoggerModule {}

apps/api/src/logger/logger.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { ConsoleLogger, Injectable } from '@nestjs/common'
2+
import { InjectSentry, SentryService } from '@ntegral/nestjs-sentry'
3+
4+
@Injectable()
5+
export class MyLogger extends ConsoleLogger {
6+
constructor(@InjectSentry() private readonly client: SentryService) {
7+
super()
8+
}
9+
10+
error(message: any, stack?: string, context?: string) {
11+
// add your tailored logic here
12+
13+
this.client.instance().captureMessage(message, 'error')
14+
super.error(message, stack, context)
15+
}
16+
}

apps/api/src/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { setupSwagger } from './config/swagger.config'
88
import { setupExceptions } from './config/exceptions.config'
99
import { setupValidation } from './config/validation.config'
1010
import { setupShutdownHooks } from './config/shutdown.config'
11+
import { MyLogger } from './logger/logger'
12+
import { LoggerModule } from './logger/logger.module'
1113

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

2729
app.setGlobalPrefix(globalPrefix)
2830
app.enableVersioning({ type: VersioningType.URI })
31+
app.useLogger(app.get(MyLogger))
2932

3033
const appVersion = process.env.APP_VERSION || 'unknown'
3134
setupHelmet(app)

apps/api/src/tasks/bank-import/import-transactions.task.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,7 @@ export class IrisTasks {
199199
// No transactions for the day yet
200200
if (!transactions.length) return
201201
} catch (e) {
202-
Logger.error(e.message)
203-
throw new BadRequestException('Failed to get transactions data from Iris' + e.message)
202+
return Logger.error('Failed to get transactions data from Iris' + e.message)
204203
}
205204

206205
// 3. Prepare the BankTransaction Records

0 commit comments

Comments
 (0)