From f4ea943c9cb2321e41705de6c5c27535a0f5eae0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Thu, 24 Oct 2024 12:53:34 +0200 Subject: [PATCH] fix(core): Ensure `LoggerProxy` is not scoped (#11379) --- .../logging/__tests__/logger.service.test.ts | 32 +++++++++++++++++++ packages/cli/src/logging/logger.service.ts | 5 +-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/logging/__tests__/logger.service.test.ts b/packages/cli/src/logging/__tests__/logger.service.test.ts index d01a709639..2ffbf2120e 100644 --- a/packages/cli/src/logging/__tests__/logger.service.test.ts +++ b/packages/cli/src/logging/__tests__/logger.service.test.ts @@ -1,10 +1,42 @@ +jest.mock('n8n-workflow', () => ({ + ...jest.requireActual('n8n-workflow'), + LoggerProxy: { init: jest.fn() }, +})); + import type { GlobalConfig } from '@n8n/config'; import { mock } from 'jest-mock-extended'; import type { InstanceSettings } from 'n8n-core'; +import { LoggerProxy } from 'n8n-workflow'; import { Logger } from '@/logging/logger.service'; describe('Logger', () => { + beforeEach(() => { + jest.resetAllMocks(); + }); + + describe('constructor', () => { + const globalConfig = mock({ + logging: { + level: 'info', + outputs: ['console'], + scopes: [], + }, + }); + + test('if root, should initialize `LoggerProxy` with instance', () => { + const logger = new Logger(globalConfig, mock(), { isRoot: true }); + + expect(LoggerProxy.init).toHaveBeenCalledWith(logger); + }); + + test('if scoped, should not initialize `LoggerProxy`', () => { + new Logger(globalConfig, mock(), { isRoot: false }); + + expect(LoggerProxy.init).not.toHaveBeenCalled(); + }); + }); + describe('transports', () => { test('if `console` selected, should set console transport', () => { const globalConfig = mock({ diff --git a/packages/cli/src/logging/logger.service.ts b/packages/cli/src/logging/logger.service.ts index 87e5fbe015..46471c0611 100644 --- a/packages/cli/src/logging/logger.service.ts +++ b/packages/cli/src/logging/logger.service.ts @@ -30,6 +30,7 @@ export class Logger { constructor( private readonly globalConfig: GlobalConfig, private readonly instanceSettings: InstanceSettings, + { isRoot }: { isRoot?: boolean } = { isRoot: true }, ) { this.level = this.globalConfig.logging.level; @@ -51,7 +52,7 @@ export class Logger { this.scopes = new Set(scopes); } - LoggerProxy.init(this); + if (isRoot) LoggerProxy.init(this); } private setInternalLogger(internalLogger: winston.Logger) { @@ -61,7 +62,7 @@ export class Logger { /** Create a logger that injects the given scopes into its log metadata. */ scoped(scopes: LogScope | LogScope[]) { scopes = Array.isArray(scopes) ? scopes : [scopes]; - const scopedLogger = new Logger(this.globalConfig, this.instanceSettings); + const scopedLogger = new Logger(this.globalConfig, this.instanceSettings, { isRoot: false }); const childLogger = this.internalLogger.child({ scopes }); scopedLogger.setInternalLogger(childLogger);