refactor(core): Modernize logger service (#11031)
This commit is contained in:
47
packages/@n8n/config/src/configs/logging.config.ts
Normal file
47
packages/@n8n/config/src/configs/logging.config.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { Config, Env, Nested } from '../decorators';
|
||||
import { StringArray } from '../utils';
|
||||
|
||||
@Config
|
||||
class FileLoggingConfig {
|
||||
/**
|
||||
* Max number of log files to keep, or max number of days to keep logs for.
|
||||
* Once the limit is reached, the oldest log files will be rotated out.
|
||||
* If using days, append a `d` suffix. Only for `file` log output.
|
||||
*
|
||||
* @example `N8N_LOG_FILE_COUNT_MAX=7` will keep at most 7 files.
|
||||
* @example `N8N_LOG_FILE_COUNT_MAX=7d` will keep at most 7 days worth of files.
|
||||
*/
|
||||
@Env('N8N_LOG_FILE_COUNT_MAX')
|
||||
fileCountMax: number = 100;
|
||||
|
||||
/** Max size (in MiB) for each log file. Only for `file` log output. */
|
||||
@Env('N8N_LOG_FILE_SIZE_MAX')
|
||||
fileSizeMax: number = 16;
|
||||
|
||||
/** Location of the log files inside `~/.n8n`. Only for `file` log output. */
|
||||
@Env('N8N_LOG_FILE_LOCATION')
|
||||
location: string = 'logs/n8n.log';
|
||||
}
|
||||
|
||||
@Config
|
||||
export class LoggingConfig {
|
||||
/**
|
||||
* Minimum level of logs to output. Logs with this or higher level will be output;
|
||||
* logs with lower levels will not. Exception: `silent` disables all logging.
|
||||
*
|
||||
* @example `N8N_LOG_LEVEL=info` will output `error`, `warn` and `info` logs, but not `debug`.
|
||||
*/
|
||||
@Env('N8N_LOG_LEVEL')
|
||||
level: 'error' | 'warn' | 'info' | 'debug' | 'silent' = 'info';
|
||||
|
||||
/**
|
||||
* Where to output logs to. Options are: `console` or `file` or both in a comma separated list.
|
||||
*
|
||||
* @example `N8N_LOG_OUTPUT=console,file` will output to both console and file.
|
||||
*/
|
||||
@Env('N8N_LOG_OUTPUT')
|
||||
outputs: StringArray<'console' | 'file'> = ['console'];
|
||||
|
||||
@Nested
|
||||
file: FileLoggingConfig;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { EndpointsConfig } from './configs/endpoints.config';
|
||||
import { EventBusConfig } from './configs/event-bus.config';
|
||||
import { ExternalSecretsConfig } from './configs/external-secrets.config';
|
||||
import { ExternalStorageConfig } from './configs/external-storage.config';
|
||||
import { LoggingConfig } from './configs/logging.config';
|
||||
import { NodesConfig } from './configs/nodes.config';
|
||||
import { PublicApiConfig } from './configs/public-api.config';
|
||||
import { ScalingModeConfig } from './configs/scaling-mode.config';
|
||||
@@ -81,4 +82,7 @@ export class GlobalConfig {
|
||||
|
||||
@Nested
|
||||
queue: ScalingModeConfig;
|
||||
|
||||
@Nested
|
||||
logging: LoggingConfig;
|
||||
}
|
||||
|
||||
7
packages/@n8n/config/src/utils.ts
Normal file
7
packages/@n8n/config/src/utils.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
export class StringArray<T extends string> extends Array<T> {
|
||||
constructor(str: string) {
|
||||
super();
|
||||
const parsed = str.split(',') as StringArray<T>;
|
||||
return parsed.every((i) => typeof i === 'string') ? parsed : [];
|
||||
}
|
||||
}
|
||||
@@ -225,6 +225,15 @@ describe('GlobalConfig', () => {
|
||||
backendDsn: '',
|
||||
frontendDsn: '',
|
||||
},
|
||||
logging: {
|
||||
level: 'info',
|
||||
outputs: ['console'],
|
||||
file: {
|
||||
fileCountMax: 100,
|
||||
fileSizeMax: 16,
|
||||
location: 'logs/n8n.log',
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
it('should use all default values when no env variables are defined', () => {
|
||||
|
||||
Reference in New Issue
Block a user