fix(core): Ensure LoggerProxy is not scoped

This commit is contained in:
Iván Ovejero 2024-10-24 09:52:39 +02:00
parent e61a8535aa
commit 9f83313506
No known key found for this signature in database
2 changed files with 35 additions and 2 deletions

View file

@ -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<GlobalConfig>({
logging: {
level: 'info',
outputs: ['console'],
scopes: [],
},
});
test('if root, should initialize `LoggerProxy` with instance', () => {
const logger = new Logger(globalConfig, mock<InstanceSettings>(), { isRoot: true });
expect(LoggerProxy.init).toHaveBeenCalledWith(logger);
});
test('if scoped, should not initialize `LoggerProxy`', () => {
new Logger(globalConfig, mock<InstanceSettings>(), { isRoot: false });
expect(LoggerProxy.init).not.toHaveBeenCalled();
});
});
describe('transports', () => {
test('if `console` selected, should set console transport', () => {
const globalConfig = mock<GlobalConfig>({

View file

@ -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);