From 3d417fdd1c6fcbf789ccb5db49823efe8436a8e2 Mon Sep 17 00:00:00 2001 From: Kim Nilsson Date: Tue, 14 Feb 2023 20:26:13 +0100 Subject: [PATCH] fix: Fix crash when setting logger to false The logger injected into the health check service was incorrectly typed, and thus assumed to have a method that the fake logger doesn't have. --- lib/health-check/health-check.service.spec.ts | 7 +++---- lib/health-check/health-check.service.ts | 7 +++++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/lib/health-check/health-check.service.spec.ts b/lib/health-check/health-check.service.spec.ts index b61260602..645727f68 100644 --- a/lib/health-check/health-check.service.spec.ts +++ b/lib/health-check/health-check.service.spec.ts @@ -4,7 +4,7 @@ import { HealthCheckExecutor } from './health-check-executor.service'; import { ERROR_LOGGER } from './error-logger/error-logger.provider'; import { ErrorLogger } from './error-logger/error-logger.interface'; import { TERMINUS_LOGGER } from './logger/logger.provider'; -import { ConsoleLogger } from '@nestjs/common'; +import { ConsoleLogger, LoggerService } from '@nestjs/common'; const healthCheckExecutorMock: Partial = { execute: jest.fn(), @@ -14,8 +14,7 @@ const errorLoggerMock: ErrorLogger = { getErrorMessage: jest.fn(), }; -const loggerMock: Partial = { - setContext: jest.fn(), +const loggerMock: Partial = { log: jest.fn(), error: jest.fn(), warn: jest.fn(), @@ -25,7 +24,7 @@ const loggerMock: Partial = { describe('HealthCheckService', () => { let healthCheckExecutor: HealthCheckExecutor; let healthCheckService: HealthCheckService; - let logger: ConsoleLogger; + let logger: LoggerService; let errorLogger: ErrorLogger; beforeEach(async () => { diff --git a/lib/health-check/health-check.service.ts b/lib/health-check/health-check.service.ts index d10e05d6b..d151a50aa 100644 --- a/lib/health-check/health-check.service.ts +++ b/lib/health-check/health-check.service.ts @@ -3,6 +3,7 @@ import { ServiceUnavailableException, Inject, ConsoleLogger, + LoggerService, } from '@nestjs/common'; import { HealthIndicatorFunction } from '../health-indicator'; import { ErrorLogger } from './error-logger/error-logger.interface'; @@ -22,9 +23,11 @@ export class HealthCheckService { @Inject(ERROR_LOGGER) private readonly errorLogger: ErrorLogger, @Inject(TERMINUS_LOGGER) - private readonly logger: ConsoleLogger, + private readonly logger: LoggerService, ) { - this.logger.setContext(HealthCheckService.name); + if (this.logger instanceof ConsoleLogger) { + this.logger.setContext(HealthCheckService.name); + } } /**