2022-02-26 20:07:39 -06:00
|
|
|
import cluster from 'node:cluster';
|
|
|
|
import chalk from 'chalk';
|
|
|
|
import { default as convertColor } from 'color-convert';
|
2022-02-03 19:41:27 -06:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
2022-12-04 00:03:09 -06:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 13:27:08 -05:00
|
|
|
import { envOption } from './env.js';
|
2019-03-02 03:51:59 -06:00
|
|
|
|
2022-12-10 00:45:30 -06:00
|
|
|
type Context = {
|
2019-03-02 03:51:59 -06:00
|
|
|
name: string;
|
2019-04-12 11:43:22 -05:00
|
|
|
color?: string;
|
2019-03-02 03:51:59 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
|
|
|
|
|
|
|
export default class Logger {
|
2022-12-10 00:45:30 -06:00
|
|
|
private context: Context;
|
2019-04-12 11:43:22 -05:00
|
|
|
private parentLogger: Logger | null = null;
|
2019-03-02 03:51:59 -06:00
|
|
|
private store: boolean;
|
2019-08-29 18:29:46 -05:00
|
|
|
private syslogClient: any | null = null;
|
2019-03-02 03:51:59 -06:00
|
|
|
|
2022-12-10 00:45:30 -06:00
|
|
|
constructor(context: string, color?: string, store = true, syslogClient = null) {
|
|
|
|
this.context = {
|
|
|
|
name: context,
|
2019-03-02 03:51:59 -06:00
|
|
|
color: color,
|
|
|
|
};
|
|
|
|
this.store = store;
|
2022-09-17 13:27:08 -05:00
|
|
|
this.syslogClient = syslogClient;
|
2019-03-02 03:51:59 -06:00
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-12-10 00:45:30 -06:00
|
|
|
public createSubLogger(context: string, color?: string, store = true): Logger {
|
|
|
|
const logger = new Logger(context, color, store);
|
2019-03-02 03:51:59 -06:00
|
|
|
logger.parentLogger = this;
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2022-12-10 00:45:30 -06:00
|
|
|
private log(level: Level, message: string, data?: Record<string, any> | null, important = false, subContexts: Context[] = [], store = true): void {
|
2021-10-08 07:24:05 -05:00
|
|
|
if (envOption.quiet) return;
|
2019-03-02 03:51:59 -06:00
|
|
|
if (!this.store) store = false;
|
2020-03-28 04:28:21 -05:00
|
|
|
if (level === 'debug') store = false;
|
2019-03-02 03:51:59 -06:00
|
|
|
|
|
|
|
if (this.parentLogger) {
|
2022-12-10 00:45:30 -06:00
|
|
|
this.parentLogger.log(level, message, data, important, [this.context].concat(subContexts), store);
|
2019-03-02 03:51:59 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-03 19:41:27 -06:00
|
|
|
const time = dateFormat(new Date(), 'HH:mm:ss');
|
2021-11-11 11:02:25 -06:00
|
|
|
const worker = cluster.isPrimary ? '*' : cluster.worker.id;
|
2019-03-02 03:51:59 -06:00
|
|
|
const l =
|
|
|
|
level === 'error' ? important ? chalk.bgRed.white('ERR ') : chalk.red('ERR ') :
|
|
|
|
level === 'warning' ? chalk.yellow('WARN') :
|
|
|
|
level === 'success' ? important ? chalk.bgGreen.white('DONE') : chalk.green('DONE') :
|
|
|
|
level === 'debug' ? chalk.gray('VERB') :
|
|
|
|
level === 'info' ? chalk.blue('INFO') :
|
|
|
|
null;
|
2022-12-10 00:45:30 -06:00
|
|
|
const contexts = [this.context].concat(subContexts).map(d => d.color ? chalk.rgb(...convertColor.keyword.rgb(d.color))(d.name) : chalk.white(d.name));
|
2019-03-02 03:51:59 -06:00
|
|
|
const m =
|
|
|
|
level === 'error' ? chalk.red(message) :
|
|
|
|
level === 'warning' ? chalk.yellow(message) :
|
|
|
|
level === 'success' ? chalk.green(message) :
|
|
|
|
level === 'debug' ? chalk.gray(message) :
|
|
|
|
level === 'info' ? message :
|
|
|
|
null;
|
|
|
|
|
2022-12-10 00:45:30 -06:00
|
|
|
let log = `${l} ${worker}\t[${contexts.join(' ')}]\t${m}`;
|
2021-10-08 07:24:05 -05:00
|
|
|
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
|
2019-03-02 03:51:59 -06:00
|
|
|
|
|
|
|
console.log(important ? chalk.bold(log) : log);
|
|
|
|
|
|
|
|
if (store) {
|
2019-08-29 18:29:46 -05:00
|
|
|
if (this.syslogClient) {
|
|
|
|
const send =
|
|
|
|
level === 'error' ? this.syslogClient.error :
|
|
|
|
level === 'warning' ? this.syslogClient.warning :
|
|
|
|
level === 'success' ? this.syslogClient.info :
|
|
|
|
level === 'debug' ? this.syslogClient.info :
|
|
|
|
level === 'info' ? this.syslogClient.info :
|
|
|
|
null as never;
|
|
|
|
|
2021-02-27 02:39:55 -06:00
|
|
|
send.bind(this.syslogClient)(message).catch(() => {});
|
2019-08-29 18:29:46 -05:00
|
|
|
}
|
2019-03-02 03:51:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2019-04-12 11:43:22 -05:00
|
|
|
public error(x: string | Error, data?: Record<string, any> | null, important = false): void { // 実行を継続できない状況で使う
|
2019-03-02 03:51:59 -06:00
|
|
|
if (x instanceof Error) {
|
2022-09-17 13:27:08 -05:00
|
|
|
data = data ?? {};
|
2019-03-02 03:51:59 -06:00
|
|
|
data.e = x;
|
|
|
|
this.log('error', x.toString(), data, important);
|
2021-02-27 02:39:55 -06:00
|
|
|
} else if (typeof x === 'object') {
|
2022-09-17 13:27:08 -05:00
|
|
|
this.log('error', `${(x as any).message ?? (x as any).name ?? x}`, data, important);
|
2019-03-02 03:51:59 -06:00
|
|
|
} else {
|
2021-02-27 02:39:55 -06:00
|
|
|
this.log('error', `${x}`, data, important);
|
2019-03-02 03:51:59 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2019-06-14 08:54:19 -05:00
|
|
|
public warn(message: string, data?: Record<string, any> | null, important = false): void { // 実行を継続できるが改善すべき状況で使う
|
2019-03-02 03:51:59 -06:00
|
|
|
this.log('warning', message, data, important);
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2019-04-12 11:43:22 -05:00
|
|
|
public succ(message: string, data?: Record<string, any> | null, important = false): void { // 何かに成功した状況で使う
|
2019-03-02 03:51:59 -06:00
|
|
|
this.log('success', message, data, important);
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2019-04-12 11:43:22 -05:00
|
|
|
public debug(message: string, data?: Record<string, any> | null, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
2022-02-03 19:41:27 -06:00
|
|
|
if (process.env.NODE_ENV !== 'production' || envOption.verbose) {
|
2019-03-02 03:51:59 -06:00
|
|
|
this.log('debug', message, data, important);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 00:03:09 -06:00
|
|
|
@bindThis
|
2019-04-12 11:43:22 -05:00
|
|
|
public info(message: string, data?: Record<string, any> | null, important = false): void { // それ以外
|
2019-03-02 03:51:59 -06:00
|
|
|
this.log('info', message, data, important);
|
|
|
|
}
|
|
|
|
}
|