2022-06-23 10:45:56 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
2022-04-08 10:37:27 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-shadow */
|
2021-08-29 11:58:11 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
2022-06-23 07:07:56 -07:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
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
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
import { 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-04-08 14:32:08 -07:00
|
|
|
import config from '../config';
|
2021-05-01 20:43:01 -07:00
|
|
|
|
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
|
|
|
|
2022-04-08 10:37:27 -07:00
|
|
|
const output = config
|
|
|
|
.getEnv('logs.output')
|
|
|
|
.split(',')
|
|
|
|
.map((output) => output.trim());
|
2021-05-01 20:43:01 -07:00
|
|
|
|
|
|
|
this.logger = winston.createLogger({
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
2021-05-01 20:43:01 -07:00
|
|
|
level,
|
2022-04-01 08:48:02 -07:00
|
|
|
silent: level === 'silent',
|
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 }),
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
2021-05-01 20:43:01 -07:00
|
|
|
winston.format.printf(({ level, message, timestamp, metadata }) => {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
|
|
|
|
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-08-29 11:58:11 -07:00
|
|
|
}`;
|
|
|
|
}),
|
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,
|
2021-08-29 11:58:11 -07:00
|
|
|
}),
|
2021-05-01 20:43:01 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output.includes('file')) {
|
|
|
|
const fileLogFormat = winston.format.combine(
|
|
|
|
winston.format.timestamp(),
|
|
|
|
winston.format.metadata(),
|
2021-08-29 11:58:11 -07:00
|
|
|
winston.format.json(),
|
2021-05-01 20:43:01 -07:00
|
|
|
);
|
|
|
|
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-08-29 11:58:11 -07:00
|
|
|
}),
|
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) {
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
2021-05-01 20:43:01 -07:00
|
|
|
logDetails.file = basename(callsite[2].getFileName() || '');
|
|
|
|
const functionName = callsite[2].getFunctionName();
|
|
|
|
if (functionName) {
|
|
|
|
logDetails.function = functionName;
|
|
|
|
}
|
|
|
|
}
|
2021-08-29 11:58:11 -07:00
|
|
|
this.logger.log(type, message, { ...meta, ...logDetails });
|
2021-05-01 20:43:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
2021-08-29 11:58:11 -07:00
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
2021-05-01 20:43:01 -07:00
|
|
|
export function getLogger() {
|
|
|
|
if (activeLoggerInstance === undefined) {
|
|
|
|
activeLoggerInstance = new Logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return activeLoggerInstance;
|
|
|
|
}
|