Changing logger to loggerService

This commit is contained in:
Collin Duncan 2023-07-29 14:58:48 +02:00
parent e7503e3074
commit d1faef35d2
No known key found for this signature in database
5 changed files with 13 additions and 21 deletions

View file

@ -7,11 +7,11 @@ import { LoggerService } from './service'
export class LoggerMiddleware implements NestMiddleware { export class LoggerMiddleware implements NestMiddleware {
constructor( constructor(
@Inject(LoggerService) @Inject(LoggerService)
private readonly logger: LoggerService, private readonly loggerService: LoggerService,
) {} ) {}
use(req: Request, _res: Response, next: NextFunction) { use(req: Request, _res: Response, next: NextFunction) {
this.logger.log(`${req.method} ${req.originalUrl}`) this.loggerService.log(`${req.method} ${req.originalUrl}`)
next() next()
} }
} }

View file

@ -1,14 +1,4 @@
import { ConsoleLogger, Injectable } from '@nestjs/common' import { ConsoleLogger, Injectable } from '@nestjs/common'
@Injectable() @Injectable()
export class LoggerService extends ConsoleLogger { export class LoggerService extends ConsoleLogger {}
log(message: any, ...optionalParams: any[]) {
super.log(message, ...optionalParams)
}
error(message: any, ...optionalParams: any[]) {
super.error(message, ...optionalParams)
}
warn(message: any, ...optionalParams: any[]) {
super.warn(message, ...optionalParams)
}
}

View file

@ -12,7 +12,7 @@ export class RecurringReservationsCronService {
private readonly recurringReservationsService: RecurringReservationsService, private readonly recurringReservationsService: RecurringReservationsService,
@Inject(LoggerService) @Inject(LoggerService)
private readonly logger: LoggerService, private readonly loggerService: LoggerService,
) {} ) {}
@Cron(CronExpression.EVERY_DAY_AT_4AM, { @Cron(CronExpression.EVERY_DAY_AT_4AM, {
@ -23,7 +23,7 @@ export class RecurringReservationsCronService {
const dayOfWeek = dayjs().get('day') const dayOfWeek = dayjs().get('day')
const recurringReservationsToSchedule = const recurringReservationsToSchedule =
await this.recurringReservationsService.getByDayOfWeek(dayOfWeek) await this.recurringReservationsService.getByDayOfWeek(dayOfWeek)
this.logger.log( this.loggerService.log(
`Found ${recurringReservationsToSchedule.length} recurring reservations to schedule`, `Found ${recurringReservationsToSchedule.length} recurring reservations to schedule`,
) )
for (const recurringReservation of recurringReservationsToSchedule) { for (const recurringReservation of recurringReservationsToSchedule) {

View file

@ -17,7 +17,7 @@ export class ReservationsCronService {
private readonly reservationsQueue: Queue, private readonly reservationsQueue: Queue,
@Inject(LoggerService) @Inject(LoggerService)
private readonly logger: LoggerService, private readonly loggerService: LoggerService,
) {} ) {}
@Cron(CronExpression.EVERY_DAY_AT_7AM, { @Cron(CronExpression.EVERY_DAY_AT_7AM, {
@ -26,7 +26,7 @@ export class ReservationsCronService {
}) })
async handleDailyReservations() { async handleDailyReservations() {
const reservationsToPerform = await this.reservationService.getByDate() const reservationsToPerform = await this.reservationService.getByDate()
this.logger.log( this.loggerService.log(
`Found ${reservationsToPerform.length} reservations to perform`, `Found ${reservationsToPerform.length} reservations to perform`,
) )
await this.reservationsQueue.addBulk( await this.reservationsQueue.addBulk(

View file

@ -18,7 +18,7 @@ export class ReservationsWorker {
private readonly brService: BaanReserverenService, private readonly brService: BaanReserverenService,
@Inject(LoggerService) @Inject(LoggerService)
private readonly logger: LoggerService, private readonly loggerService: LoggerService,
) {} ) {}
@Process() @Process()
@ -26,7 +26,7 @@ export class ReservationsWorker {
const reservation = plainToInstance(Reservation, job.data, { const reservation = plainToInstance(Reservation, job.data, {
groups: ['password'], groups: ['password'],
}) })
this.logger.log('Handling reservation', { this.loggerService.log('Handling reservation', {
reservation: instanceToPlain(reservation), reservation: instanceToPlain(reservation),
}) })
await this.performReservation(reservation) await this.performReservation(reservation)
@ -38,12 +38,14 @@ export class ReservationsWorker {
) { ) {
switch (true) { switch (true) {
case error instanceof NoCourtAvailableError: { case error instanceof NoCourtAvailableError: {
this.logger.warn('No court available, adding to waiting list') this.loggerService.warn('No court available, adding to waiting list')
await this.addReservationToWaitList(reservation) await this.addReservationToWaitList(reservation)
return return
} }
default: default:
this.logger.error('Error while performing reservation', { error }) this.loggerService.error('Error while performing reservation', {
error,
})
throw error throw error
} }
} }