From b53bf571fe72286ee4d09b43fdd84884c9c15988 Mon Sep 17 00:00:00 2001 From: sbriat Date: Tue, 7 Feb 2023 18:31:53 +0100 Subject: [PATCH] add territory logs --- src/modules/logger.module.ts | 8 +- .../primaries/territory.controller.ts | 109 ++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 src/modules/logger/adapters/primaries/territory.controller.ts diff --git a/src/modules/logger.module.ts b/src/modules/logger.module.ts index 8a1e30c..1acd4be 100644 --- a/src/modules/logger.module.ts +++ b/src/modules/logger.module.ts @@ -6,6 +6,7 @@ import { WinstonModule } from 'nest-winston'; import * as winston from 'winston'; import { AuthController } from './logger/adapters/primaries/auth.controller'; import { ConfigurationController } from './logger/adapters/primaries/configuration.controller'; +import { TerritoryController } from './logger/adapters/primaries/territory.controller'; @Module({ imports: [ @@ -28,7 +29,12 @@ import { ConfigurationController } from './logger/adapters/primaries/configurati transports: [], }), ], - controllers: [AuthController, UserController, ConfigurationController], + controllers: [ + AuthController, + UserController, + ConfigurationController, + TerritoryController, + ], providers: [], exports: [], }) diff --git a/src/modules/logger/adapters/primaries/territory.controller.ts b/src/modules/logger/adapters/primaries/territory.controller.ts new file mode 100644 index 0000000..1d22e13 --- /dev/null +++ b/src/modules/logger/adapters/primaries/territory.controller.ts @@ -0,0 +1,109 @@ +import { RabbitSubscribe } from '@golevelup/nestjs-rabbitmq'; +import { Controller, Inject } from '@nestjs/common'; +import { WINSTON_MODULE_PROVIDER } from 'nest-winston'; +import { Logger } from 'winston'; +import { level } from './logger/level.enum'; +import loggerOptions from './logger/logger'; + +@Controller() +export class TerritoryController { + constructor( + @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, + ) {} + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.create.info', + queue: 'logging-territory-create-info', + }) + public async territoryCreatedInfoHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.info, 'info', 'create'), + ); + this.logger.info(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.create.warning', + queue: 'logging-territory-create-warning', + }) + public async territoryCreatedWarningHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.warning, 'warning', 'create'), + ); + this.logger.warning(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.create.crit', + queue: 'logging-territory-create-crit', + }) + public async territoryCreatedCriticalHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.crit, 'critical', 'create'), + ); + this.logger.crit(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.delete.info', + queue: 'logging-territory-delete-info', + }) + public async territoryDeletedInfoHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.info, 'info', 'delete'), + ); + this.logger.info(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.delete.crit', + queue: 'logging-territory-delete-crit', + }) + public async territoryDeletedCriticalHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.crit, 'critical', 'delete'), + ); + this.logger.crit(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.read.warning', + queue: 'logging-territory-read-warning', + }) + public async territoryReadWarningHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.warning, 'warning', 'read'), + ); + this.logger.warning(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.update.info', + queue: 'logging-territory-update-info', + }) + public async territoryUpdatedInfoHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.info, 'info', 'update'), + ); + this.logger.info(JSON.parse(message)); + } + + @RabbitSubscribe({ + exchange: 'logging', + routingKey: 'territory.update.crit', + queue: 'logging-territory-update-crit', + }) + public async territoryUpdatedCriticalHandler(message: string) { + this.logger.configure( + loggerOptions('territory', level.crit, 'critical', 'update'), + ); + this.logger.crit(JSON.parse(message)); + } +}