Adding new database logger for logging database related things
This commit is contained in:
parent
09de8ecff3
commit
135b9930f6
12 changed files with 98 additions and 14 deletions
|
|
@ -8,6 +8,7 @@ import { resolve } from 'path'
|
||||||
import { EmailModule } from './email/module'
|
import { EmailModule } from './email/module'
|
||||||
import { LoggerMiddleware } from './logger/middleware'
|
import { LoggerMiddleware } from './logger/middleware'
|
||||||
import { LoggerModule } from './logger/module'
|
import { LoggerModule } from './logger/module'
|
||||||
|
import { DatabaseLoggerService } from './logger/service.database_logger'
|
||||||
import { RecurringReservationsModule } from './recurringReservations/module'
|
import { RecurringReservationsModule } from './recurringReservations/module'
|
||||||
import { ReservationsModule } from './reservations/module'
|
import { ReservationsModule } from './reservations/module'
|
||||||
import { RunnerModule } from './runner/module'
|
import { RunnerModule } from './runner/module'
|
||||||
|
|
@ -17,9 +18,12 @@ import { WaitingListModule } from './waitingList/module'
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot({ isGlobal: true }),
|
ConfigModule.forRoot({ isGlobal: true }),
|
||||||
TypeOrmModule.forRootAsync({
|
TypeOrmModule.forRootAsync({
|
||||||
imports: [ConfigModule],
|
imports: [ConfigModule, LoggerModule],
|
||||||
inject: [ConfigService],
|
inject: [ConfigService, DatabaseLoggerService],
|
||||||
useFactory: (configService: ConfigService) => ({
|
useFactory: (
|
||||||
|
configService: ConfigService,
|
||||||
|
databaseLoggerService: DatabaseLoggerService,
|
||||||
|
) => ({
|
||||||
type: 'sqlite',
|
type: 'sqlite',
|
||||||
database: configService.get<string>(
|
database: configService.get<string>(
|
||||||
'DATABASE',
|
'DATABASE',
|
||||||
|
|
@ -28,6 +32,7 @@ import { WaitingListModule } from './waitingList/module'
|
||||||
migrations: [],
|
migrations: [],
|
||||||
autoLoadEntities: true,
|
autoLoadEntities: true,
|
||||||
logging: true,
|
logging: true,
|
||||||
|
logger: databaseLoggerService,
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
BullModule.forRootAsync({
|
BullModule.forRootAsync({
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { ConfigService } from '@nestjs/config'
|
||||||
import * as Imap from 'imap'
|
import * as Imap from 'imap'
|
||||||
import { MailParser, ParsedEmail } from 'mailparser-mit'
|
import { MailParser, ParsedEmail } from 'mailparser-mit'
|
||||||
|
|
||||||
import { LoggerService } from '../logger/service'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { Email } from './types'
|
import { Email } from './types'
|
||||||
|
|
||||||
export enum EmailClientStatus {
|
export enum EmailClientStatus {
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
import { Inject, Injectable, NestMiddleware } from '@nestjs/common'
|
import { Inject, Injectable, NestMiddleware } from '@nestjs/common'
|
||||||
import { NextFunction, Request, Response } from 'express'
|
import { NextFunction, Request, Response } from 'express'
|
||||||
|
|
||||||
import { LoggerService } from './service'
|
import { LoggerService } from './service.logger'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class LoggerMiddleware implements NestMiddleware {
|
export class LoggerMiddleware implements NestMiddleware {
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,11 @@
|
||||||
import { Module } from '@nestjs/common'
|
import { Module } from '@nestjs/common'
|
||||||
|
|
||||||
import { LoggerMiddleware } from './middleware'
|
import { LoggerMiddleware } from './middleware'
|
||||||
import { LoggerService } from './service'
|
import { DatabaseLoggerService } from './service.database_logger'
|
||||||
|
import { LoggerService } from './service.logger'
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
providers: [LoggerService, LoggerMiddleware],
|
providers: [LoggerService, DatabaseLoggerService, LoggerMiddleware],
|
||||||
exports: [LoggerService, LoggerMiddleware],
|
exports: [LoggerService, DatabaseLoggerService, LoggerMiddleware],
|
||||||
})
|
})
|
||||||
export class LoggerModule {}
|
export class LoggerModule {}
|
||||||
|
|
|
||||||
72
src/logger/service.database_logger.ts
Normal file
72
src/logger/service.database_logger.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
/* eslint-disable @typescript-eslint/no-unused-vars */
|
||||||
|
import { Inject, Injectable } from '@nestjs/common'
|
||||||
|
import { Logger, QueryRunner } from 'typeorm'
|
||||||
|
|
||||||
|
import { LoggerService } from './service.logger'
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class DatabaseLoggerService implements Logger {
|
||||||
|
constructor(
|
||||||
|
@Inject(LoggerService)
|
||||||
|
private loggerService: LoggerService,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
logQuery(
|
||||||
|
query: string,
|
||||||
|
parameters?: any[] | undefined,
|
||||||
|
_queryRunner?: QueryRunner | undefined,
|
||||||
|
) {
|
||||||
|
this.loggerService.debug('Query', `${query} - ${parameters?.join(',')}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
logQueryError(
|
||||||
|
error: string | Error,
|
||||||
|
query: string,
|
||||||
|
parameters?: any[] | undefined,
|
||||||
|
_queryRunner?: QueryRunner | undefined,
|
||||||
|
) {
|
||||||
|
this.loggerService.error(
|
||||||
|
'Query error',
|
||||||
|
`${error}: ${query} - ${parameters?.join(',')}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
logQuerySlow(
|
||||||
|
time: number,
|
||||||
|
query: string,
|
||||||
|
parameters?: any[] | undefined,
|
||||||
|
_queryRunner?: QueryRunner | undefined,
|
||||||
|
) {
|
||||||
|
this.loggerService.warn(
|
||||||
|
'Slow query',
|
||||||
|
`${query} - ${parameters?.join(',')} took ${time}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
logSchemaBuild(message: string, _queryRunner?: QueryRunner | undefined) {
|
||||||
|
this.log('info', message)
|
||||||
|
}
|
||||||
|
|
||||||
|
logMigration(message: string, _queryRunner?: QueryRunner | undefined) {
|
||||||
|
this.log('info', message)
|
||||||
|
}
|
||||||
|
|
||||||
|
log(
|
||||||
|
level: 'log' | 'info' | 'warn',
|
||||||
|
message: any,
|
||||||
|
_queryRunner?: QueryRunner | undefined,
|
||||||
|
) {
|
||||||
|
let logFn: (message: any) => void
|
||||||
|
switch (level) {
|
||||||
|
case 'warn':
|
||||||
|
logFn = this.loggerService.warn
|
||||||
|
break
|
||||||
|
case 'log':
|
||||||
|
case 'info':
|
||||||
|
default:
|
||||||
|
logFn = this.loggerService.log
|
||||||
|
}
|
||||||
|
|
||||||
|
logFn(message)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'
|
||||||
import { Cron, CronExpression } from '@nestjs/schedule'
|
import { Cron, CronExpression } from '@nestjs/schedule'
|
||||||
|
|
||||||
import dayjs from '../common/dayjs'
|
import dayjs from '../common/dayjs'
|
||||||
import { LoggerService } from '../logger/service'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { RecurringReservationsService } from './service'
|
import { RecurringReservationsService } from './service'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
|
@ -20,6 +20,7 @@ export class RecurringReservationsCronService {
|
||||||
timeZone: 'Europe/Amsterdam',
|
timeZone: 'Europe/Amsterdam',
|
||||||
})
|
})
|
||||||
async handleRecurringReservations() {
|
async handleRecurringReservations() {
|
||||||
|
this.loggerService.log('handleRecurringReservations beginning')
|
||||||
const dayOfWeek = dayjs().get('day')
|
const dayOfWeek = dayjs().get('day')
|
||||||
const recurringReservationsToSchedule =
|
const recurringReservationsToSchedule =
|
||||||
await this.recurringReservationsService.getByDayOfWeek(dayOfWeek)
|
await this.recurringReservationsService.getByDayOfWeek(dayOfWeek)
|
||||||
|
|
@ -31,5 +32,6 @@ export class RecurringReservationsCronService {
|
||||||
recurringReservation,
|
recurringReservation,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
this.loggerService.log('handleRecurringReservations ending')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import {
|
||||||
import { Queue } from 'bull'
|
import { Queue } from 'bull'
|
||||||
import { Dayjs } from 'dayjs'
|
import { Dayjs } from 'dayjs'
|
||||||
|
|
||||||
import { LoggerService } from '../logger/service'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { RESERVATIONS_QUEUE_NAME } from './config'
|
import { RESERVATIONS_QUEUE_NAME } from './config'
|
||||||
import { Reservation } from './entity'
|
import { Reservation } from './entity'
|
||||||
import { ReservationsService } from './service'
|
import { ReservationsService } from './service'
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { Cron, CronExpression } from '@nestjs/schedule'
|
||||||
import { Queue } from 'bull'
|
import { Queue } from 'bull'
|
||||||
|
|
||||||
import dayjs from '../common/dayjs'
|
import dayjs from '../common/dayjs'
|
||||||
import { LoggerService } from '../logger/service'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { RESERVATIONS_QUEUE_NAME } from './config'
|
import { RESERVATIONS_QUEUE_NAME } from './config'
|
||||||
import { ReservationsService } from './service'
|
import { ReservationsService } from './service'
|
||||||
|
|
||||||
|
|
@ -26,6 +26,7 @@ export class ReservationsCronService {
|
||||||
timeZone: 'Europe/Amsterdam',
|
timeZone: 'Europe/Amsterdam',
|
||||||
})
|
})
|
||||||
async handleDailyReservations() {
|
async handleDailyReservations() {
|
||||||
|
this.loggerService.log('handleDailyReservations beginning')
|
||||||
const reservationsToPerform = await this.reservationService.getByDate(
|
const reservationsToPerform = await this.reservationService.getByDate(
|
||||||
dayjs().subtract(7, 'days'),
|
dayjs().subtract(7, 'days'),
|
||||||
)
|
)
|
||||||
|
|
@ -35,6 +36,7 @@ export class ReservationsCronService {
|
||||||
await this.reservationsQueue.addBulk(
|
await this.reservationsQueue.addBulk(
|
||||||
reservationsToPerform.map((r) => ({ data: r, opts: { attempts: 1 } })),
|
reservationsToPerform.map((r) => ({ data: r, opts: { attempts: 1 } })),
|
||||||
)
|
)
|
||||||
|
this.loggerService.log('handleDailyReservations ending')
|
||||||
}
|
}
|
||||||
|
|
||||||
@Cron(CronExpression.EVERY_DAY_AT_11PM, {
|
@Cron(CronExpression.EVERY_DAY_AT_11PM, {
|
||||||
|
|
@ -42,6 +44,7 @@ export class ReservationsCronService {
|
||||||
timeZone: 'Europe/Amsterdam',
|
timeZone: 'Europe/Amsterdam',
|
||||||
})
|
})
|
||||||
async cleanUpExpiredReservations() {
|
async cleanUpExpiredReservations() {
|
||||||
|
this.loggerService.log('cleanUpExpiredReservations beginning')
|
||||||
const reservations = await this.reservationService.getByDate()
|
const reservations = await this.reservationService.getByDate()
|
||||||
this.loggerService.log(
|
this.loggerService.log(
|
||||||
`Found ${reservations.length} reservations to delete`,
|
`Found ${reservations.length} reservations to delete`,
|
||||||
|
|
@ -49,5 +52,6 @@ export class ReservationsCronService {
|
||||||
for (const reservation of reservations) {
|
for (const reservation of reservations) {
|
||||||
await this.reservationService.deleteById(reservation.id)
|
await this.reservationService.deleteById(reservation.id)
|
||||||
}
|
}
|
||||||
|
this.loggerService.log('cleanUpExpiredReservations ending')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ import { Inject } from '@nestjs/common'
|
||||||
import { Job } from 'bull'
|
import { Job } from 'bull'
|
||||||
import { instanceToPlain, plainToInstance } from 'class-transformer'
|
import { instanceToPlain, plainToInstance } from 'class-transformer'
|
||||||
|
|
||||||
import { LoggerService } from '../logger/service'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import {
|
import {
|
||||||
BaanReserverenService,
|
BaanReserverenService,
|
||||||
NoCourtAvailableError,
|
NoCourtAvailableError,
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'
|
||||||
import { instanceToPlain } from 'class-transformer'
|
import { instanceToPlain } from 'class-transformer'
|
||||||
import { Dayjs } from 'dayjs'
|
import { Dayjs } from 'dayjs'
|
||||||
import { ElementHandle, Page } from 'puppeteer'
|
import { ElementHandle, Page } from 'puppeteer'
|
||||||
import { LoggerService } from 'src/logger/service'
|
import { LoggerService } from 'src/logger/service.logger'
|
||||||
|
|
||||||
import dayjs from '../../common/dayjs'
|
import dayjs from '../../common/dayjs'
|
||||||
import { Reservation } from '../../reservations/entity'
|
import { Reservation } from '../../reservations/entity'
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import { ReservationsService } from 'src/reservations/service'
|
||||||
import dayjs from '../common/dayjs'
|
import dayjs from '../common/dayjs'
|
||||||
import { EMAILS_QUEUE_NAME } from '../email/config'
|
import { EMAILS_QUEUE_NAME } from '../email/config'
|
||||||
import { Email } from '../email/types'
|
import { Email } from '../email/types'
|
||||||
import { LoggerService } from '../logger/service'
|
import { LoggerService } from '../logger/service.logger'
|
||||||
import { RESERVATIONS_QUEUE_NAME } from '../reservations/config'
|
import { RESERVATIONS_QUEUE_NAME } from '../reservations/config'
|
||||||
import { WaitingListDetails } from './types'
|
import { WaitingListDetails } from './types'
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue