From 6a0917805cc4bbc02053a67750115cb173eb5503 Mon Sep 17 00:00:00 2001 From: Johan Book <{ID}+{username}@users.noreply.github.com> Date: Wed, 26 Jul 2023 20:30:06 +0200 Subject: [PATCH] chore(api): add unhandled exception logger --- .../api/src/core/logging/logging.module.ts | 3 ++- .../logging/unhandled-exception.logger.ts | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 services/api/src/core/logging/unhandled-exception.logger.ts diff --git a/services/api/src/core/logging/logging.module.ts b/services/api/src/core/logging/logging.module.ts index 4f4ae61b..5c88d9be 100644 --- a/services/api/src/core/logging/logging.module.ts +++ b/services/api/src/core/logging/logging.module.ts @@ -3,11 +3,12 @@ import { CqrsModule } from "@nestjs/cqrs"; import { CommandLogger } from "./command.logger"; import { EventLogger } from "./event.logger"; +import { UnhandledExceptionLogger } from "./unhandled-exception.logger"; @Module({ exports: [], imports: [CqrsModule], controllers: [], - providers: [CommandLogger, EventLogger], + providers: [CommandLogger, EventLogger, UnhandledExceptionLogger], }) export class LoggingModule {} diff --git a/services/api/src/core/logging/unhandled-exception.logger.ts b/services/api/src/core/logging/unhandled-exception.logger.ts new file mode 100644 index 00000000..c7f8b753 --- /dev/null +++ b/services/api/src/core/logging/unhandled-exception.logger.ts @@ -0,0 +1,19 @@ +import { Injectable } from "@nestjs/common"; +import { UnhandledExceptionBus } from "@nestjs/cqrs"; + +import { Logger } from "./domain/services/logger.service"; + +@Injectable() +export class UnhandledExceptionLogger { + private logger = new Logger(UnhandledExceptionLogger.name); + + constructor(private readonly unhandledExceptionBus: UnhandledExceptionBus) { + this.unhandledExceptionBus.subscribe((exceptionInfo) => { + this.logger.error({ + cause: exceptionInfo.cause, + error: exceptionInfo.exception, + msg: `Encountered unhandled exception ${exceptionInfo.exception.message}`, + }); + }); + } +}