2022-04-08 10:37:27 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-shadow */
|
2022-06-25 21:03:46 -07:00
|
|
|
|
2022-06-23 07:05:20 -07:00
|
|
|
import { inspect } from 'util';
|
2022-04-08 14:32:08 -07:00
|
|
|
import winston from 'winston';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2023-01-27 05:56:56 -08:00
|
|
|
import type { IDataObject, ILogger, LogTypes } from 'n8n-workflow';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import callsites from 'callsites';
|
2021-05-01 20:43:01 -07:00
|
|
|
import { basename } from 'path';
|
2022-11-09 06:25:00 -08:00
|
|
|
import config from '@/config';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
2023-10-20 09:26:33 -07:00
|
|
|
const noOp = () => {};
|
|
|
|
const levelNames = ['debug', 'verbose', 'info', 'warn', 'error'] as const;
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
export class Logger implements ILogger {
|
2021-05-01 20:43:01 -07:00
|
|
|
private logger: winston.Logger;
|
|
|
|
|
|
|
|
constructor() {
|
2022-04-08 10:37:27 -07:00
|
|
|
const level = config.getEnv('logs.level');
|
2022-04-01 08:48:02 -07:00
|
|
|
|
2021-05-01 20:43:01 -07:00
|
|
|
this.logger = winston.createLogger({
|
|
|
|
level,
|
2022-04-01 08:48:02 -07:00
|
|
|
silent: level === 'silent',
|
2021-05-01 20:43:01 -07:00
|
|
|
});
|
|
|
|
|
2023-10-20 09:26:33 -07:00
|
|
|
// Change all methods with higher log-level to no-op
|
|
|
|
for (const levelName of levelNames) {
|
|
|
|
if (this.logger.levels[levelName] > this.logger.levels[level]) {
|
|
|
|
Object.defineProperty(this, levelName, { value: noOp });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const output = config
|
|
|
|
.getEnv('logs.output')
|
|
|
|
.split(',')
|
|
|
|
.map((output) => output.trim());
|
|
|
|
|
2021-05-01 20:43:01 -07:00
|
|
|
if (output.includes('console')) {
|
|
|
|
let format: winston.Logform.Format;
|
|
|
|
if (['debug', 'verbose'].includes(level)) {
|
|
|
|
format = winston.format.combine(
|
|
|
|
winston.format.metadata(),
|
|
|
|
winston.format.timestamp(),
|
|
|
|
winston.format.colorize({ all: true }),
|
2023-07-31 02:00:48 -07:00
|
|
|
|
2021-05-01 20:43:01 -07:00
|
|
|
winston.format.printf(({ level, message, timestamp, metadata }) => {
|
|
|
|
return `${timestamp} | ${level.padEnd(18)} | ${message}${
|
2022-07-24 08:25:01 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
2022-06-23 07:05:20 -07:00
|
|
|
Object.keys(metadata).length ? ` ${JSON.stringify(inspect(metadata))}` : ''
|
2021-05-01 20:43:01 -07:00
|
|
|
}`;
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
} else {
|
2022-06-23 07:05:20 -07:00
|
|
|
format = winston.format.printf(({ message }: { message: string }) => message);
|
2021-05-01 20:43:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
this.logger.add(
|
|
|
|
new winston.transports.Console({
|
|
|
|
format,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.includes('file')) {
|
|
|
|
const fileLogFormat = winston.format.combine(
|
|
|
|
winston.format.timestamp(),
|
|
|
|
winston.format.metadata(),
|
|
|
|
winston.format.json(),
|
|
|
|
);
|
|
|
|
this.logger.add(
|
|
|
|
new winston.transports.File({
|
2022-04-08 10:37:27 -07:00
|
|
|
filename: config.getEnv('logs.file.location'),
|
2021-05-01 20:43:01 -07:00
|
|
|
format: fileLogFormat,
|
2022-04-08 10:37:27 -07:00
|
|
|
maxsize: config.getEnv('logs.file.fileSizeMax') * 1048576, // config * 1mb
|
|
|
|
maxFiles: config.getEnv('logs.file.fileCountMax'),
|
2021-05-01 20:43:01 -07:00
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
log(type: LogTypes, message: string, meta: object = {}): void {
|
2021-05-01 20:43:01 -07:00
|
|
|
const callsite = callsites();
|
|
|
|
// We are using the third array element as the structure is as follows:
|
|
|
|
// [0]: this file
|
|
|
|
// [1]: Should be LoggerProxy
|
|
|
|
// [2]: Should point to the caller.
|
|
|
|
// Note: getting line number is useless because at this point
|
|
|
|
// We are in runtime, so it means we are looking at compiled js files
|
|
|
|
const logDetails = {} as IDataObject;
|
|
|
|
if (callsite[2] !== undefined) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
|
|
logDetails.file = basename(callsite[2].getFileName() || '');
|
|
|
|
const functionName = callsite[2].getFunctionName();
|
|
|
|
if (functionName) {
|
|
|
|
logDetails.function = functionName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.logger.log(type, message, { ...meta, ...logDetails });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convenience methods below
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
debug(message: string, meta: object = {}): void {
|
2021-05-01 20:43:01 -07:00
|
|
|
this.log('debug', message, meta);
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
info(message: string, meta: object = {}): void {
|
2021-05-01 20:43:01 -07:00
|
|
|
this.log('info', message, meta);
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
error(message: string, meta: object = {}): void {
|
2021-05-01 20:43:01 -07:00
|
|
|
this.log('error', message, meta);
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
verbose(message: string, meta: object = {}): void {
|
2021-05-01 20:43:01 -07:00
|
|
|
this.log('verbose', message, meta);
|
|
|
|
}
|
|
|
|
|
2022-06-25 21:03:46 -07:00
|
|
|
warn(message: string, meta: object = {}): void {
|
2021-05-01 20:43:01 -07:00
|
|
|
this.log('warn', message, meta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let activeLoggerInstance: Logger | undefined;
|
|
|
|
|
|
|
|
export function getLogger() {
|
|
|
|
if (activeLoggerInstance === undefined) {
|
|
|
|
activeLoggerInstance = new Logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return activeLoggerInstance;
|
|
|
|
}
|