mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 08:34:07 -08:00
fix(core): Ensure LoggerProxy
is not scoped
This commit is contained in:
parent
e61a8535aa
commit
9f83313506
|
@ -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>({
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in a new issue