mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-21 11:49:59 +00:00
fix(core): Honor absolute paths for N8N_LOG_FILE_LOCATION (#15873)
This commit is contained in:
@@ -4,8 +4,9 @@ jest.mock('n8n-workflow', () => ({
|
|||||||
}));
|
}));
|
||||||
|
|
||||||
import type { GlobalConfig, InstanceSettingsConfig } from '@n8n/config';
|
import type { GlobalConfig, InstanceSettingsConfig } from '@n8n/config';
|
||||||
import { mock } from 'jest-mock-extended';
|
import { mock, captor } from 'jest-mock-extended';
|
||||||
import { LoggerProxy } from 'n8n-workflow';
|
import { LoggerProxy } from 'n8n-workflow';
|
||||||
|
import winston from 'winston';
|
||||||
|
|
||||||
import { Logger } from '../logger';
|
import { Logger } from '../logger';
|
||||||
|
|
||||||
@@ -37,6 +38,10 @@ describe('Logger', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('transports', () => {
|
describe('transports', () => {
|
||||||
|
afterEach(() => {
|
||||||
|
jest.restoreAllMocks();
|
||||||
|
});
|
||||||
|
|
||||||
test('if `console` selected, should set console transport', () => {
|
test('if `console` selected, should set console transport', () => {
|
||||||
const globalConfig = mock<GlobalConfig>({
|
const globalConfig = mock<GlobalConfig>({
|
||||||
logging: {
|
logging: {
|
||||||
@@ -57,7 +62,8 @@ describe('Logger', () => {
|
|||||||
expect(transport.constructor.name).toBe('Console');
|
expect(transport.constructor.name).toBe('Console');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('if `file` selected, should set file transport', () => {
|
describe('`file`', () => {
|
||||||
|
test('should set file transport', () => {
|
||||||
const globalConfig = mock<GlobalConfig>({
|
const globalConfig = mock<GlobalConfig>({
|
||||||
logging: {
|
logging: {
|
||||||
level: 'info',
|
level: 'info',
|
||||||
@@ -71,7 +77,10 @@ describe('Logger', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const logger = new Logger(globalConfig, mock<InstanceSettingsConfig>({ n8nFolder: '/tmp' }));
|
const logger = new Logger(
|
||||||
|
globalConfig,
|
||||||
|
mock<InstanceSettingsConfig>({ n8nFolder: '/tmp' }),
|
||||||
|
);
|
||||||
|
|
||||||
const { transports } = logger.getInternalLogger();
|
const { transports } = logger.getInternalLogger();
|
||||||
|
|
||||||
@@ -81,6 +90,66 @@ describe('Logger', () => {
|
|||||||
|
|
||||||
expect(transport.constructor.name).toBe('File');
|
expect(transport.constructor.name).toBe('File');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('should accept absolute paths', () => {
|
||||||
|
// ARRANGE
|
||||||
|
const location = '/tmp/n8n.log';
|
||||||
|
const globalConfig = mock<GlobalConfig>({
|
||||||
|
logging: {
|
||||||
|
level: 'info',
|
||||||
|
outputs: ['file'],
|
||||||
|
scopes: [],
|
||||||
|
file: { fileSizeMax: 100, fileCountMax: 16, location },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const OriginalFile = winston.transports.File;
|
||||||
|
const FileSpy = jest.spyOn(winston.transports, 'File').mockImplementation((...args) => {
|
||||||
|
return new OriginalFile(...args);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ACT
|
||||||
|
new Logger(globalConfig, mock<InstanceSettingsConfig>({ n8nFolder: '/tmp' }));
|
||||||
|
|
||||||
|
// ASSERT
|
||||||
|
const fileOptionsCaptor = captor<string>();
|
||||||
|
|
||||||
|
expect(FileSpy).toHaveBeenCalledTimes(1);
|
||||||
|
expect(FileSpy).toHaveBeenCalledWith(fileOptionsCaptor);
|
||||||
|
expect(fileOptionsCaptor.value).toMatchObject({ filename: location });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should accept relative paths', () => {
|
||||||
|
// ARRANGE
|
||||||
|
const location = 'tmp/n8n.log';
|
||||||
|
const n8nFolder = '/tmp/n8n';
|
||||||
|
const globalConfig = mock<GlobalConfig>({
|
||||||
|
logging: {
|
||||||
|
level: 'info',
|
||||||
|
outputs: ['file'],
|
||||||
|
scopes: [],
|
||||||
|
file: {
|
||||||
|
fileSizeMax: 100,
|
||||||
|
fileCountMax: 16,
|
||||||
|
location,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const OriginalFile = winston.transports.File;
|
||||||
|
const FileSpy = jest.spyOn(winston.transports, 'File').mockImplementation((...args) => {
|
||||||
|
return new OriginalFile(...args);
|
||||||
|
});
|
||||||
|
|
||||||
|
// ACT
|
||||||
|
new Logger(globalConfig, mock<InstanceSettingsConfig>({ n8nFolder }));
|
||||||
|
|
||||||
|
// ASSERT
|
||||||
|
const fileOptionsCaptor = captor<string>();
|
||||||
|
|
||||||
|
expect(FileSpy).toHaveBeenCalledTimes(1);
|
||||||
|
expect(FileSpy).toHaveBeenCalledWith(fileOptionsCaptor);
|
||||||
|
expect(fileOptionsCaptor.value).toMatchObject({ filename: `${n8nFolder}/${location}` });
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('levels', () => {
|
describe('levels', () => {
|
||||||
|
|||||||
@@ -195,10 +195,9 @@ export class Logger implements LoggerType {
|
|||||||
winston.format.json(),
|
winston.format.json(),
|
||||||
);
|
);
|
||||||
|
|
||||||
const filename = path.join(
|
const filename = path.isAbsolute(this.globalConfig.logging.file.location)
|
||||||
this.instanceSettingsConfig.n8nFolder,
|
? this.globalConfig.logging.file.location
|
||||||
this.globalConfig.logging.file.location,
|
: path.join(this.instanceSettingsConfig.n8nFolder, this.globalConfig.logging.file.location);
|
||||||
);
|
|
||||||
|
|
||||||
const { fileSizeMax, fileCountMax } = this.globalConfig.logging.file;
|
const { fileSizeMax, fileCountMax } = this.globalConfig.logging.file;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user