From d7ec0e7da0ebb20cd7dc084c7b5392450c23ab23 Mon Sep 17 00:00:00 2001 From: sbriat Date: Thu, 29 Jun 2023 11:49:07 +0200 Subject: [PATCH] refactor --- README.md | 10 +- src/app.module.ts | 4 +- .../{adapters/primaries => }/health.module.ts | 2 +- .../health.http.controller.ts} | 0 src/modules/logger.module.ts | 236 -------------- .../logger => interface}/level.enum.ts | 0 .../logger.ts => interface/logger.options.ts} | 0 .../logger => interface}/loglevel.filter.ts | 0 .../message-controllers}/ad.controller.ts | 32 +- .../admin-api.controller.ts | 4 +- .../message-controllers}/auth.controller.ts | 28 +- .../configuration.controller.ts | 32 +- .../gateway-api.controller.ts | 4 +- .../matcher.controller.ts | 14 +- .../territory.controller.ts | 32 +- .../message-controllers}/user.controller.ts | 32 +- .../logger => interface}/transport.options.ts | 0 src/modules/logger/logger.module.ts | 287 ++++++++++++++++++ tsconfig.json | 6 +- 19 files changed, 394 insertions(+), 329 deletions(-) rename src/modules/health/{adapters/primaries => }/health.module.ts (74%) rename src/modules/health/{adapters/primaries/health.controller.ts => interface/http-controllers/health.http.controller.ts} (100%) delete mode 100644 src/modules/logger.module.ts rename src/modules/logger/{adapters/primaries/logger => interface}/level.enum.ts (100%) rename src/modules/logger/{adapters/primaries/logger/logger.ts => interface/logger.options.ts} (100%) rename src/modules/logger/{adapters/primaries/logger => interface}/loglevel.filter.ts (100%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/ad.controller.ts (77%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/admin-api.controller.ts (87%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/auth.controller.ts (78%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/configuration.controller.ts (85%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/gateway-api.controller.ts (87%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/matcher.controller.ts (63%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/territory.controller.ts (80%) rename src/modules/logger/{adapters/primaries => interface/message-controllers}/user.controller.ts (76%) rename src/modules/logger/{adapters/primaries/logger => interface}/transport.options.ts (100%) create mode 100644 src/modules/logger/logger.module.ts diff --git a/README.md b/README.md index 6d430ca..4f94543 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ The app runs automatically on the port defined in `SERVICE_PORT` of `.env` file The app subscribes to RabbitMQ queues in order to log messages. We use the routing key with the given format : <**service**>.<**action**>.<**level**>, with : - **service** : the name of the service that has emitted the message -- **action** : the action that was at the origin of the message (one or more words separated by a dot) +- **action** : the action that triggered the message (one or more words separated by a dot, last word should be ) - **level** : the severity (_log level_) of the message, as described in [RFC5424 syslog](https://www.rfc-editor.org/rfc/rfc5424) : - **emer** : Emergency: system is unusable - **alert** : Alert: action must be taken immediately @@ -44,10 +44,10 @@ The app subscribes to RabbitMQ queues in order to log messages. We use the routi Examples of valid routing keys : -- user.create.info -- user.create.crit -- user.update.warning -- auth.username.add.info +- user.created.info +- user.created.crit +- user.updated.warning +- auth.username.added.info It is the responsibility of each service to set the routing key to the appropriate value. diff --git a/src/app.module.ts b/src/app.module.ts index 85ca2e3..e66015c 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,7 +1,7 @@ +import { HealthModule } from '@modules/health/health.module'; +import { LoggerModule } from '@modules/logger/logger.module'; import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; -import { LoggerModule } from './modules/logger.module'; -import { HealthModule } from './modules/health/adapters/primaries/health.module'; @Module({ imports: [ diff --git a/src/modules/health/adapters/primaries/health.module.ts b/src/modules/health/health.module.ts similarity index 74% rename from src/modules/health/adapters/primaries/health.module.ts rename to src/modules/health/health.module.ts index c835ee2..29d2fe7 100644 --- a/src/modules/health/adapters/primaries/health.module.ts +++ b/src/modules/health/health.module.ts @@ -1,7 +1,7 @@ import { Module } from '@nestjs/common'; import { TerminusModule } from '@nestjs/terminus'; import { HttpModule } from '@nestjs/axios'; -import { HealthController } from './health.controller'; +import { HealthController } from './interface/http-controllers/health.http.controller'; @Module({ imports: [TerminusModule, HttpModule], diff --git a/src/modules/health/adapters/primaries/health.controller.ts b/src/modules/health/interface/http-controllers/health.http.controller.ts similarity index 100% rename from src/modules/health/adapters/primaries/health.controller.ts rename to src/modules/health/interface/http-controllers/health.http.controller.ts diff --git a/src/modules/logger.module.ts b/src/modules/logger.module.ts deleted file mode 100644 index 011bb47..0000000 --- a/src/modules/logger.module.ts +++ /dev/null @@ -1,236 +0,0 @@ -import { Module } from '@nestjs/common'; -import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; -import { ConfigModule, ConfigService } from '@nestjs/config'; -import { UserController } from './logger/adapters/primaries/user.controller'; -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'; -import { GatewayApiController } from './logger/adapters/primaries/gateway-api.controller'; -import { AdminApiController } from './logger/adapters/primaries/admin-api.controller'; -import { MatcherController } from './logger/adapters/primaries/matcher.controller'; -import { AdController } from './logger/adapters/primaries/ad.controller'; - -@Module({ - imports: [ - RabbitMQModule.forRootAsync(RabbitMQModule, { - imports: [ConfigModule], - useFactory: async (configService: ConfigService) => ({ - exchanges: [ - { - name: configService.get('RMQ_EXCHANGE'), - type: 'topic', - }, - ], - handlers: { - loggingGatewayApiHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.gateway-api.health.crit', - }, - loggingAdminApiHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.admin-api.health.crit', - }, - loggingAdCreateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.create.info', - }, - loggingAdCreateWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.create.warning', - }, - loggingAdCreateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.create.crit', - }, - loggingAdDeleteInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.delete.info', - }, - loggingAdDeleteCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.delete.crit', - }, - loggingAdReadWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.read.warning', - }, - loggingAdUpdateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.update.info', - }, - loggingAdUpdateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.update.crit', - }, - loggingAdHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.ad.health.crit', - }, - loggingAuthCreateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.create.crit', - }, - loggingAuthDeleteCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.delete.crit', - }, - loggingAuthUsernameAddWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.username.add.warning', - }, - loggingAuthUsernameDeleteWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.username.delete.warning', - }, - loggingAuthPasswordUpdateWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.password.update.warning', - }, - loggingAuthUsernameUpdateWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.username.update.warning', - }, - loggingAuthHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.auth.health.crit', - }, - loggingConfigurationCreateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.create.info', - }, - loggingConfigurationCreateWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.create.warning', - }, - loggingConfigurationCreateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.create.crit', - }, - loggingConfigurationDeleteInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.delete.info', - }, - loggingConfigurationDeleteCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.delete.crit', - }, - loggingConfigurationReadWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.read.warning', - }, - loggingConfigurationUpdateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.update.info', - }, - loggingConfigurationUpdateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.update.crit', - }, - loggingConfigurationHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.configuration.health.crit', - }, - loggingMatcherMatchCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.matcher.match.crit', - }, - loggingTerritoryCreateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.create.info', - }, - loggingTerritoryCreateWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.create.warning', - }, - loggingTerritoryCreateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.create.crit', - }, - loggingTerritoryDeleteInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.delete.info', - }, - loggingTerritoryDeleteCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.delete.crit', - }, - loggingTerritoryReadWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.read.warning', - }, - loggingTerritoryUpdateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.update.info', - }, - loggingTerritoryUpdateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.update.crit', - }, - loggingTerritoryHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.territory.health.crit', - }, - loggingUserCreateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.create.info', - }, - loggingUserCreateWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.create.warning', - }, - loggingUserCreateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.create.crit', - }, - loggingUserDeleteInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.delete.info', - }, - loggingUserDeleteCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.delete.crit', - }, - loggingUserReadWarning: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.read.warning', - }, - loggingUserUpdateInfo: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.update.info', - }, - loggingUserUpdateCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.update.crit', - }, - loggingUserHealthCrit: { - exchange: configService.get('RMQ_EXCHANGE'), - routingKey: 'logging.user.health.crit', - }, - }, - uri: configService.get('RMQ_URI'), - connectionInitOptions: { wait: false }, - enableControllerDiscovery: true, - }), - inject: [ConfigService], - }), - WinstonModule.forRoot({ - levels: winston.config.syslog.levels, - transports: [], - }), - ], - controllers: [ - AdController, - AuthController, - UserController, - ConfigurationController, - MatcherController, - TerritoryController, - GatewayApiController, - AdminApiController, - ], - providers: [], - exports: [], -}) -export class LoggerModule {} diff --git a/src/modules/logger/adapters/primaries/logger/level.enum.ts b/src/modules/logger/interface/level.enum.ts similarity index 100% rename from src/modules/logger/adapters/primaries/logger/level.enum.ts rename to src/modules/logger/interface/level.enum.ts diff --git a/src/modules/logger/adapters/primaries/logger/logger.ts b/src/modules/logger/interface/logger.options.ts similarity index 100% rename from src/modules/logger/adapters/primaries/logger/logger.ts rename to src/modules/logger/interface/logger.options.ts diff --git a/src/modules/logger/adapters/primaries/logger/loglevel.filter.ts b/src/modules/logger/interface/loglevel.filter.ts similarity index 100% rename from src/modules/logger/adapters/primaries/logger/loglevel.filter.ts rename to src/modules/logger/interface/loglevel.filter.ts diff --git a/src/modules/logger/adapters/primaries/ad.controller.ts b/src/modules/logger/interface/message-controllers/ad.controller.ts similarity index 77% rename from src/modules/logger/adapters/primaries/ad.controller.ts rename to src/modules/logger/interface/message-controllers/ad.controller.ts index c0fd7f9..dc5040f 100644 --- a/src/modules/logger/adapters/primaries/ad.controller.ts +++ b/src/modules/logger/interface/message-controllers/ad.controller.ts @@ -2,8 +2,8 @@ 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'; +import { level } from '../level.enum'; +import loggerOptions from '../logger.options'; @Controller() export class AdController { @@ -12,47 +12,47 @@ export class AdController { ) {} @RabbitSubscribe({ - name: 'loggingAdCreateInfo', + name: 'loggingAdCreatedInfo', }) public async adCreatedInfoHandler(message: string) { - this.logger.configure(loggerOptions('ad', level.info, 'info', 'create')); + this.logger.configure(loggerOptions('ad', level.info, 'info', 'created')); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAdCreateWarning', + name: 'loggingAdCreatedWarning', }) public async adCreatedWarningHandler(message: string) { this.logger.configure( - loggerOptions('ad', level.warning, 'warning', 'create'), + loggerOptions('ad', level.warning, 'warning', 'created'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAdCreateCrit', + name: 'loggingAdCreatedCrit', }) public async adCreatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('ad', level.crit, 'critical', 'create'), + loggerOptions('ad', level.crit, 'critical', 'created'), ); this.logger.crit(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAdDeleteInfo', + name: 'loggingAdDeletedInfo', }) public async adDeletedInfoHandler(message: string) { - this.logger.configure(loggerOptions('ad', level.info, 'info', 'delete')); + this.logger.configure(loggerOptions('ad', level.info, 'info', 'deleted')); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAdDeleteCrit', + name: 'loggingAdDeletedCrit', }) public async adDeletedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('ad', level.crit, 'critical', 'delete'), + loggerOptions('ad', level.crit, 'critical', 'deleted'), ); this.logger.crit(JSON.parse(message)); } @@ -68,19 +68,19 @@ export class AdController { } @RabbitSubscribe({ - name: 'loggingAdUpdateInfo', + name: 'loggingAdUpdatedInfo', }) public async adUpdatedInfoHandler(message: string) { - this.logger.configure(loggerOptions('ad', level.info, 'info', 'update')); + this.logger.configure(loggerOptions('ad', level.info, 'info', 'updated')); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAdUpdateCrit', + name: 'loggingAdUpdatedCrit', }) public async adUpdatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('ad', level.crit, 'critical', 'update'), + loggerOptions('ad', level.crit, 'critical', 'updated'), ); this.logger.crit(JSON.parse(message)); } diff --git a/src/modules/logger/adapters/primaries/admin-api.controller.ts b/src/modules/logger/interface/message-controllers/admin-api.controller.ts similarity index 87% rename from src/modules/logger/adapters/primaries/admin-api.controller.ts rename to src/modules/logger/interface/message-controllers/admin-api.controller.ts index 1dad810..a73fbff 100644 --- a/src/modules/logger/adapters/primaries/admin-api.controller.ts +++ b/src/modules/logger/interface/message-controllers/admin-api.controller.ts @@ -2,8 +2,8 @@ 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'; +import loggerOptions from '../logger.options'; +import { level } from '../level.enum'; @Controller() export class AdminApiController { diff --git a/src/modules/logger/adapters/primaries/auth.controller.ts b/src/modules/logger/interface/message-controllers/auth.controller.ts similarity index 78% rename from src/modules/logger/adapters/primaries/auth.controller.ts rename to src/modules/logger/interface/message-controllers/auth.controller.ts index f9823c7..3ab6472 100644 --- a/src/modules/logger/adapters/primaries/auth.controller.ts +++ b/src/modules/logger/interface/message-controllers/auth.controller.ts @@ -2,8 +2,8 @@ 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'; +import loggerOptions from '../logger.options'; +import { level } from '../level.enum'; @Controller() export class AuthController { @@ -12,61 +12,61 @@ export class AuthController { ) {} @RabbitSubscribe({ - name: 'loggingAuthCreateCrit', + name: 'loggingAuthCreatedCrit', }) public async authCreatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('auth', level.crit, 'critical', 'create'), + loggerOptions('auth', level.crit, 'critical', 'created'), ); this.logger.crit(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAuthDeleteCrit', + name: 'loggingAuthDeletedCrit', }) public async authDeletedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('auth', level.crit, 'critical', 'delete'), + loggerOptions('auth', level.crit, 'critical', 'deleted'), ); this.logger.crit(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAuthUsernameAddWarning', + name: 'loggingAuthUsernameAddedWarning', }) public async authUsernameAddedWarningHandler(message: string) { this.logger.configure( - loggerOptions('auth', level.warning, 'warning', 'username-add'), + loggerOptions('auth', level.warning, 'warning', 'username-added'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAuthUsernameDeleteWarning', + name: 'loggingAuthUsernameDeletedWarning', }) public async authUsernameDeletedWarningHandler(message: string) { this.logger.configure( - loggerOptions('auth', level.warning, 'warning', 'username-delete'), + loggerOptions('auth', level.warning, 'warning', 'username-deleted'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAuthPasswordUpdateWarning', + name: 'loggingAuthPasswordUpdatedWarning', }) public async authPasswordUpdatedWarningHandler(message: string) { this.logger.configure( - loggerOptions('auth', level.warning, 'warning', 'password-update'), + loggerOptions('auth', level.warning, 'warning', 'password-updated'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingAuthUsernameUpdateWarning', + name: 'loggingAuthUsernameUpdatedWarning', }) public async authUsernameUpdatedWarningHandler(message: string) { this.logger.configure( - loggerOptions('auth', level.warning, 'warning', 'username-update'), + loggerOptions('auth', level.warning, 'warning', 'username-updated'), ); this.logger.warning(JSON.parse(message)); } diff --git a/src/modules/logger/adapters/primaries/configuration.controller.ts b/src/modules/logger/interface/message-controllers/configuration.controller.ts similarity index 85% rename from src/modules/logger/adapters/primaries/configuration.controller.ts rename to src/modules/logger/interface/message-controllers/configuration.controller.ts index a48e43b..330d458 100644 --- a/src/modules/logger/adapters/primaries/configuration.controller.ts +++ b/src/modules/logger/interface/message-controllers/configuration.controller.ts @@ -2,8 +2,8 @@ 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'; +import loggerOptions from '../logger.options'; +import { level } from '../level.enum'; @Controller() export class ConfigurationController { @@ -12,51 +12,51 @@ export class ConfigurationController { ) {} @RabbitSubscribe({ - name: 'loggingConfigurationCreateInfo', + name: 'loggingConfigurationCreatedInfo', }) public async configurationCreatedInfoHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.info, 'info', 'create'), + loggerOptions('configuration', level.info, 'info', 'created'), ); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingConfigurationCreateWarning', + name: 'loggingConfigurationCreatedWarning', }) public async configurationCreatedWarningHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.warning, 'warning', 'create'), + loggerOptions('configuration', level.warning, 'warning', 'created'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingConfigurationCreateCrit', + name: 'loggingConfigurationCreatedCrit', }) public async configurationCreatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.crit, 'critical', 'create'), + loggerOptions('configuration', level.crit, 'critical', 'created'), ); this.logger.crit(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingConfigurationDeleteInfo', + name: 'loggingConfigurationDeletedInfo', }) public async configurationDeletedInfoHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.info, 'info', 'delete'), + loggerOptions('configuration', level.info, 'info', 'deleted'), ); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingConfigurationDeleteCrit', + name: 'loggingConfigurationDeletedCrit', }) public async configurationDeletedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.crit, 'critical', 'delete'), + loggerOptions('configuration', level.crit, 'critical', 'deleted'), ); this.logger.crit(JSON.parse(message)); } @@ -72,21 +72,21 @@ export class ConfigurationController { } @RabbitSubscribe({ - name: 'loggingConfigurationUpdateInfo', + name: 'loggingConfigurationUpdatedInfo', }) public async configurationUpdatedInfoHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.info, 'info', 'update'), + loggerOptions('configuration', level.info, 'info', 'updated'), ); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingConfigurationUpdateCrit', + name: 'loggingConfigurationUpdatedCrit', }) public async configurationUpdatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('configuration', level.crit, 'critical', 'update'), + loggerOptions('configuration', level.crit, 'critical', 'updated'), ); this.logger.crit(JSON.parse(message)); } diff --git a/src/modules/logger/adapters/primaries/gateway-api.controller.ts b/src/modules/logger/interface/message-controllers/gateway-api.controller.ts similarity index 87% rename from src/modules/logger/adapters/primaries/gateway-api.controller.ts rename to src/modules/logger/interface/message-controllers/gateway-api.controller.ts index 6a45503..73b1ea1 100644 --- a/src/modules/logger/adapters/primaries/gateway-api.controller.ts +++ b/src/modules/logger/interface/message-controllers/gateway-api.controller.ts @@ -2,8 +2,8 @@ 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'; +import loggerOptions from '../logger.options'; +import { level } from '../level.enum'; @Controller() export class GatewayApiController { diff --git a/src/modules/logger/adapters/primaries/matcher.controller.ts b/src/modules/logger/interface/message-controllers/matcher.controller.ts similarity index 63% rename from src/modules/logger/adapters/primaries/matcher.controller.ts rename to src/modules/logger/interface/message-controllers/matcher.controller.ts index 0398de2..dab2bf7 100644 --- a/src/modules/logger/adapters/primaries/matcher.controller.ts +++ b/src/modules/logger/interface/message-controllers/matcher.controller.ts @@ -2,8 +2,8 @@ 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'; +import { level } from '../level.enum'; +import loggerOptions from '../logger.options'; @Controller() export class MatcherController { @@ -11,6 +11,16 @@ export class MatcherController { @Inject(WINSTON_MODULE_PROVIDER) private readonly logger: Logger, ) {} + @RabbitSubscribe({ + name: 'loggingMatcherAdCrit', + }) + public async matcherAdCriticalHandler(message: string) { + this.logger.configure( + loggerOptions('matcher', level.crit, 'critical', 'ad'), + ); + this.logger.crit(JSON.parse(message)); + } + @RabbitSubscribe({ name: 'loggingMatcherMatchCrit', }) diff --git a/src/modules/logger/adapters/primaries/territory.controller.ts b/src/modules/logger/interface/message-controllers/territory.controller.ts similarity index 80% rename from src/modules/logger/adapters/primaries/territory.controller.ts rename to src/modules/logger/interface/message-controllers/territory.controller.ts index eb1caf3..298bb51 100644 --- a/src/modules/logger/adapters/primaries/territory.controller.ts +++ b/src/modules/logger/interface/message-controllers/territory.controller.ts @@ -2,8 +2,8 @@ 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'; +import loggerOptions from '../logger.options'; +import { level } from '../level.enum'; @Controller() export class TerritoryController { @@ -12,51 +12,51 @@ export class TerritoryController { ) {} @RabbitSubscribe({ - name: 'loggingTerritoryCreateInfo', + name: 'loggingTerritoryCreatedInfo', }) public async territoryCreatedInfoHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.info, 'info', 'create'), + loggerOptions('territory', level.info, 'info', 'created'), ); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingTerritoryCreateWarning', + name: 'loggingTerritoryCreatedWarning', }) public async territoryCreatedWarningHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.warning, 'warning', 'create'), + loggerOptions('territory', level.warning, 'warning', 'created'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingTerritoryCreateCrit', + name: 'loggingTerritoryCreatedCrit', }) public async territoryCreatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.crit, 'critical', 'create'), + loggerOptions('territory', level.crit, 'critical', 'created'), ); this.logger.crit(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingTerritoryDeleteInfo', + name: 'loggingTerritoryDeletedInfo', }) public async territoryDeletedInfoHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.info, 'info', 'delete'), + loggerOptions('territory', level.info, 'info', 'deleted'), ); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingTerritoryDeleteCrit', + name: 'loggingTerritoryDeletedCrit', }) public async territoryDeletedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.crit, 'critical', 'delete'), + loggerOptions('territory', level.crit, 'critical', 'deleted'), ); this.logger.crit(JSON.parse(message)); } @@ -72,21 +72,21 @@ export class TerritoryController { } @RabbitSubscribe({ - name: 'loggingTerritoryUpdateInfo', + name: 'loggingTerritoryUpdatedInfo', }) public async territoryUpdatedInfoHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.info, 'info', 'update'), + loggerOptions('territory', level.info, 'info', 'updated'), ); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingTerritoryUpdateCrit', + name: 'loggingTerritoryUpdatedCrit', }) public async territoryUpdatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('territory', level.crit, 'critical', 'update'), + loggerOptions('territory', level.crit, 'critical', 'updated'), ); this.logger.crit(JSON.parse(message)); } diff --git a/src/modules/logger/adapters/primaries/user.controller.ts b/src/modules/logger/interface/message-controllers/user.controller.ts similarity index 76% rename from src/modules/logger/adapters/primaries/user.controller.ts rename to src/modules/logger/interface/message-controllers/user.controller.ts index d10abd9..30b334e 100644 --- a/src/modules/logger/adapters/primaries/user.controller.ts +++ b/src/modules/logger/interface/message-controllers/user.controller.ts @@ -2,8 +2,8 @@ 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'; +import loggerOptions from '../logger.options'; +import { level } from '../level.enum'; @Controller() export class UserController { @@ -12,47 +12,47 @@ export class UserController { ) {} @RabbitSubscribe({ - name: 'loggingUserCreateInfo', + name: 'loggingUserCreatedInfo', }) public async userCreatedInfoHandler(message: string) { - this.logger.configure(loggerOptions('user', level.info, 'info', 'create')); + this.logger.configure(loggerOptions('user', level.info, 'info', 'created')); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingUserCreateWarning', + name: 'loggingUserCreatedWarning', }) public async userCreatedWarningHandler(message: string) { this.logger.configure( - loggerOptions('user', level.warning, 'warning', 'create'), + loggerOptions('user', level.warning, 'warning', 'created'), ); this.logger.warning(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingUserCreateCrit', + name: 'loggingUserCreatedCrit', }) public async userCreatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('user', level.crit, 'critical', 'create'), + loggerOptions('user', level.crit, 'critical', 'created'), ); this.logger.crit(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingUserDeleteInfo', + name: 'loggingUserDeletedInfo', }) public async userDeletedInfoHandler(message: string) { - this.logger.configure(loggerOptions('user', level.info, 'info', 'delete')); + this.logger.configure(loggerOptions('user', level.info, 'info', 'deleted')); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingUserDeleteCrit', + name: 'loggingUserDeletedCrit', }) public async userDeletedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('user', level.crit, 'critical', 'delete'), + loggerOptions('user', level.crit, 'critical', 'deleted'), ); this.logger.crit(JSON.parse(message)); } @@ -68,19 +68,19 @@ export class UserController { } @RabbitSubscribe({ - name: 'loggingUserUpdateInfo', + name: 'loggingUserUpdatedInfo', }) public async userUpdatedInfoHandler(message: string) { - this.logger.configure(loggerOptions('user', level.info, 'info', 'update')); + this.logger.configure(loggerOptions('user', level.info, 'info', 'updated')); this.logger.info(JSON.parse(message)); } @RabbitSubscribe({ - name: 'loggingUserUpdateCrit', + name: 'loggingUserUpdatedCrit', }) public async userUpdatedCriticalHandler(message: string) { this.logger.configure( - loggerOptions('user', level.crit, 'critical', 'update'), + loggerOptions('user', level.crit, 'critical', 'updated'), ); this.logger.crit(JSON.parse(message)); } diff --git a/src/modules/logger/adapters/primaries/logger/transport.options.ts b/src/modules/logger/interface/transport.options.ts similarity index 100% rename from src/modules/logger/adapters/primaries/logger/transport.options.ts rename to src/modules/logger/interface/transport.options.ts diff --git a/src/modules/logger/logger.module.ts b/src/modules/logger/logger.module.ts new file mode 100644 index 0000000..23c2307 --- /dev/null +++ b/src/modules/logger/logger.module.ts @@ -0,0 +1,287 @@ +import { Module } from '@nestjs/common'; +import { RabbitMQModule } from '@golevelup/nestjs-rabbitmq'; +import { ConfigModule, ConfigService } from '@nestjs/config'; +import { WinstonModule } from 'nest-winston'; +import * as winston from 'winston'; +import { AdController } from './interface/message-controllers/ad.controller'; +import { AuthController } from './interface/message-controllers/auth.controller'; +import { UserController } from './interface/message-controllers/user.controller'; +import { ConfigurationController } from './interface/message-controllers/configuration.controller'; +import { MatcherController } from './interface/message-controllers/matcher.controller'; +import { TerritoryController } from './interface/message-controllers/territory.controller'; +import { GatewayApiController } from './interface/message-controllers/gateway-api.controller'; +import { AdminApiController } from './interface/message-controllers/admin-api.controller'; + +@Module({ + imports: [ + RabbitMQModule.forRootAsync(RabbitMQModule, { + imports: [ConfigModule], + useFactory: async (configService: ConfigService) => ({ + exchanges: [ + { + name: configService.get('RMQ_EXCHANGE'), + type: 'topic', + }, + ], + handlers: { + loggingGatewayApiHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.gateway-api.health.crit', + queue: 'logging-gateway-api-health-crit', + }, + loggingAdminApiHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.admin-api.health.crit', + queue: 'logging-admin-api-health-crit', + }, + loggingAdCreatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.created.info', + queue: 'logging-ad-created-info', + }, + loggingAdCreatedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.created.warning', + queue: 'logging-ad-created-warning', + }, + loggingAdCreatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.created.crit', + queue: 'logging-ad-created-crit', + }, + loggingAdDeletedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.deleted.info', + queue: 'logging-ad-deleted-info', + }, + loggingAdDeletedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.deleted.crit', + queue: 'logging-ad-deleted-crit', + }, + loggingAdReadWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.read.warning', + queue: 'logging-ad-read-warning', + }, + loggingAdUpdatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.updated.info', + queue: 'logging-ad-updated-info', + }, + loggingAdUpdatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.updated.crit', + queue: 'logging-ad-updated-crit', + }, + loggingAdHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.ad.health.crit', + queue: 'logging-ad-health-crit', + }, + loggingAuthCreatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.created.crit', + queue: 'logging-auth-created-crit', + }, + loggingAuthDeletedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.deleted.crit', + queue: 'logging-auth-deleted-crit', + }, + loggingAuthUsernameAddedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.username.added.warning', + queue: 'logging-auth-username-added-warning', + }, + loggingAuthUsernameDeletedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.username.deleted.warning', + queue: 'logging-auth-username-deleted-warning', + }, + loggingAuthPasswordUpdatedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.password.updated.warning', + queue: 'logging-auth-password-updated-warning', + }, + loggingAuthUsernameUpdatedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.username.updated.warning', + queue: 'logging-auth-username-updated-warning', + }, + loggingAuthHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.auth.health.crit', + queue: 'logging-auth-health-crit', + }, + loggingConfigurationCreatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.created.info', + queue: 'logging-configuration-created-info', + }, + loggingConfigurationCreatedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.created.warning', + queue: 'logging-configuration-created-warning', + }, + loggingConfigurationCreatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.created.crit', + queue: 'logging-configuration-created-crit', + }, + loggingConfigurationDeletedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.deleted.info', + queue: 'logging-configuration-deleted-info', + }, + loggingConfigurationDeletedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.deleted.crit', + queue: 'logging-configuration-deleted-crit', + }, + loggingConfigurationReadWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.read.warning', + queue: 'logging-configuration-read-warning', + }, + loggingConfigurationUpdatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.updated.info', + queue: 'logging-configuration-updated-info', + }, + loggingConfigurationUpdatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.updated.crit', + queue: 'logging-configuration-updated-crit', + }, + loggingConfigurationHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.configuration.health.crit', + queue: 'logging-configuration-health-crit', + }, + loggingMatcherAdCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.matcher.ad.crit', + queue: 'logging-matcher-ad-crit', + }, + loggingMatcherMatchCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.matcher.match.crit', + queue: 'logging-matcher-match-crit', + }, + loggingTerritoryCreatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.created.info', + queue: 'logging-territory-created-info', + }, + loggingTerritoryCreatedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.created.warning', + queue: 'logging-territory-created-warning', + }, + loggingTerritoryCreatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.created.crit', + queue: 'logging-territory-created-crit', + }, + loggingTerritoryDeletedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.deleted.info', + queue: 'logging-territory-deleted-info', + }, + loggingTerritoryDeletedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.deleted.crit', + queue: 'logging-territory-deleted-crit', + }, + loggingTerritoryReadWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.read.warning', + queue: 'logging-territory-read-warning', + }, + loggingTerritoryUpdatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.updated.info', + queue: 'logging-territory-updated-info', + }, + loggingTerritoryUpdatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.updated.crit', + queue: 'logging-territory-updated-crit', + }, + loggingTerritoryHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.territory.health.crit', + queue: 'logging-territory-health-crit', + }, + loggingUserCreatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.created.info', + queue: 'logging-user-created-info', + }, + loggingUserCreatedWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.created.warning', + queue: 'logging-user-created-warning', + }, + loggingUserCreatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.created.crit', + queue: 'logging-user-created-crit', + }, + loggingUserDeletedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.deleted.info', + queue: 'logging-user-deleted-info', + }, + loggingUserDeletedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.deleted.crit', + queue: 'logging-user-deleted-crit', + }, + loggingUserReadWarning: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.read.warning', + queue: 'logging-user-read-warning', + }, + loggingUserUpdatedInfo: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.updated.info', + queue: 'logging-user-updated-info', + }, + loggingUserUpdatedCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.updated.crit', + queue: 'logging-user-updated-crit', + }, + loggingUserHealthCrit: { + exchange: configService.get('RMQ_EXCHANGE'), + routingKey: 'logging.user.health.crit', + queue: 'logging-user-health-crit', + }, + }, + uri: configService.get('RMQ_URI'), + connectionInitOptions: { wait: false }, + enableControllerDiscovery: true, + }), + inject: [ConfigService], + }), + WinstonModule.forRoot({ + levels: winston.config.syslog.levels, + transports: [], + }), + ], + controllers: [ + AdController, + AuthController, + UserController, + ConfigurationController, + MatcherController, + TerritoryController, + GatewayApiController, + AdminApiController, + ], + providers: [], + exports: [], +}) +export class LoggerModule {} diff --git a/tsconfig.json b/tsconfig.json index adb614c..0e31118 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,6 +16,10 @@ "noImplicitAny": false, "strictBindCallApply": false, "forceConsistentCasingInFileNames": false, - "noFallthroughCasesInSwitch": false + "noFallthroughCasesInSwitch": false, + "paths": { + "@modules/*": ["src/modules/*"], + "@src/*": ["src/*"] + } } }