mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-14 16:44: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 type { GlobalConfig } from '@n8n/config';
|
||||||
import { mock } from 'jest-mock-extended';
|
import { mock } from 'jest-mock-extended';
|
||||||
import type { InstanceSettings } from 'n8n-core';
|
import type { InstanceSettings } from 'n8n-core';
|
||||||
|
import { LoggerProxy } from 'n8n-workflow';
|
||||||
|
|
||||||
import { Logger } from '@/logging/logger.service';
|
import { Logger } from '@/logging/logger.service';
|
||||||
|
|
||||||
describe('Logger', () => {
|
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', () => {
|
describe('transports', () => {
|
||||||
test('if `console` selected, should set console transport', () => {
|
test('if `console` selected, should set console transport', () => {
|
||||||
const globalConfig = mock<GlobalConfig>({
|
const globalConfig = mock<GlobalConfig>({
|
||||||
|
|
|
@ -30,6 +30,7 @@ export class Logger {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly globalConfig: GlobalConfig,
|
private readonly globalConfig: GlobalConfig,
|
||||||
private readonly instanceSettings: InstanceSettings,
|
private readonly instanceSettings: InstanceSettings,
|
||||||
|
{ isRoot }: { isRoot?: boolean } = { isRoot: true },
|
||||||
) {
|
) {
|
||||||
this.level = this.globalConfig.logging.level;
|
this.level = this.globalConfig.logging.level;
|
||||||
|
|
||||||
|
@ -51,7 +52,7 @@ export class Logger {
|
||||||
this.scopes = new Set(scopes);
|
this.scopes = new Set(scopes);
|
||||||
}
|
}
|
||||||
|
|
||||||
LoggerProxy.init(this);
|
if (isRoot) LoggerProxy.init(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private setInternalLogger(internalLogger: winston.Logger) {
|
private setInternalLogger(internalLogger: winston.Logger) {
|
||||||
|
@ -61,7 +62,7 @@ export class Logger {
|
||||||
/** Create a logger that injects the given scopes into its log metadata. */
|
/** Create a logger that injects the given scopes into its log metadata. */
|
||||||
scoped(scopes: LogScope | LogScope[]) {
|
scoped(scopes: LogScope | LogScope[]) {
|
||||||
scopes = Array.isArray(scopes) ? scopes : [scopes];
|
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 });
|
const childLogger = this.internalLogger.child({ scopes });
|
||||||
|
|
||||||
scopedLogger.setInternalLogger(childLogger);
|
scopedLogger.setInternalLogger(childLogger);
|
||||||
|
|
Loading…
Reference in a new issue