2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
import cluster from 'node:cluster';
|
2024-01-15 18:29:53 +01:00
|
|
|
import util from 'node:util';
|
2022-02-27 03:07:39 +01:00
|
|
|
import chalk from 'chalk';
|
|
|
|
import { default as convertColor } from 'color-convert';
|
2022-02-04 02:41:27 +01:00
|
|
|
import { format as dateFormat } from 'date-fns';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { envOption } from './env.js';
|
2023-06-25 14:13:15 +02:00
|
|
|
import type { KEYWORD } from 'color-convert/conversions.js';
|
2019-03-02 10:51:59 +01:00
|
|
|
|
2024-01-15 18:29:53 +01:00
|
|
|
util.inspect.defaultOptions = envOption.logJson ? {
|
|
|
|
showHidden: false,
|
|
|
|
depth: null,
|
|
|
|
colors: false,
|
|
|
|
customInspect: true,
|
|
|
|
showProxy: false,
|
|
|
|
maxArrayLength: null,
|
|
|
|
maxStringLength: null,
|
|
|
|
breakLength: Infinity,
|
|
|
|
compact: true,
|
|
|
|
sorted: false,
|
|
|
|
getters: false,
|
|
|
|
numericSeparator: false,
|
|
|
|
} : {
|
|
|
|
showHidden: false,
|
|
|
|
depth: null,
|
|
|
|
colors: true,
|
|
|
|
customInspect: true,
|
|
|
|
showProxy: false,
|
|
|
|
maxArrayLength: null,
|
|
|
|
maxStringLength: null,
|
|
|
|
breakLength: Infinity,
|
|
|
|
compact: true,
|
|
|
|
sorted: false,
|
|
|
|
getters: false,
|
|
|
|
numericSeparator: false,
|
|
|
|
};
|
|
|
|
|
2022-12-10 07:45:30 +01:00
|
|
|
type Context = {
|
2019-03-02 10:51:59 +01:00
|
|
|
name: string;
|
2022-12-13 16:01:45 +01:00
|
|
|
color?: KEYWORD;
|
2019-03-02 10:51:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
|
|
|
|
2024-01-15 18:29:53 +01:00
|
|
|
function inspect(_: string, value: any): null | string | number | boolean {
|
|
|
|
if (value === null || value === undefined) return null;
|
|
|
|
if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') return value;
|
|
|
|
if (value instanceof Date) return value.toISOString();
|
|
|
|
return util.inspect(value);
|
|
|
|
}
|
|
|
|
|
2023-08-17 14:20:58 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-03-02 10:51:59 +01:00
|
|
|
export default class Logger {
|
2022-12-10 07:45:30 +01:00
|
|
|
private context: Context;
|
2019-04-12 18:43:22 +02:00
|
|
|
private parentLogger: Logger | null = null;
|
2019-03-02 10:51:59 +01:00
|
|
|
private store: boolean;
|
|
|
|
|
2023-02-03 07:08:36 +01:00
|
|
|
constructor(context: string, color?: KEYWORD, store = true) {
|
2022-12-10 07:45:30 +01:00
|
|
|
this.context = {
|
|
|
|
name: context,
|
2019-03-02 10:51:59 +01:00
|
|
|
color: color,
|
|
|
|
};
|
|
|
|
this.store = store;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-12-13 16:01:45 +01:00
|
|
|
public createSubLogger(context: string, color?: KEYWORD, store = true): Logger {
|
2022-12-10 07:45:30 +01:00
|
|
|
const logger = new Logger(context, color, store);
|
2019-03-02 10:51:59 +01:00
|
|
|
logger.parentLogger = this;
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-12-10 07:45:30 +01:00
|
|
|
private log(level: Level, message: string, data?: Record<string, any> | null, important = false, subContexts: Context[] = [], store = true): void {
|
2024-01-08 12:26:39 +01:00
|
|
|
if (envOption.quiet && !envOption.logJson) return;
|
2019-03-02 10:51:59 +01:00
|
|
|
if (!this.store) store = false;
|
2020-03-28 10:28:21 +01:00
|
|
|
if (level === 'debug') store = false;
|
2019-03-02 10:51:59 +01:00
|
|
|
|
|
|
|
if (this.parentLogger) {
|
2022-12-10 07:45:30 +01:00
|
|
|
this.parentLogger.log(level, message, data, important, [this.context].concat(subContexts), store);
|
2019-03-02 10:51:59 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-01-08 12:26:39 +01:00
|
|
|
if (envOption.logJson) {
|
|
|
|
console.log(JSON.stringify({
|
|
|
|
time: new Date().toISOString(),
|
|
|
|
level: level,
|
|
|
|
message: message,
|
|
|
|
data: data,
|
|
|
|
important: important,
|
2024-01-09 03:21:37 +01:00
|
|
|
context: [this.context].concat(subContexts).map(d => d.name).join('.'),
|
2024-01-08 12:26:39 +01:00
|
|
|
cluster: cluster.isPrimary ? 'primary' : `worker-${cluster.worker!.id}`,
|
2024-01-15 18:29:53 +01:00
|
|
|
}, inspect));
|
2024-01-08 12:26:39 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-04 02:41:27 +01:00
|
|
|
const time = dateFormat(new Date(), 'HH:mm:ss');
|
2023-02-09 03:46:08 +01:00
|
|
|
const worker = cluster.isPrimary ? '*' : cluster.worker!.id;
|
2019-03-02 10:51:59 +01: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 07:45:30 +01: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 10:51:59 +01: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 07:45:30 +01:00
|
|
|
let log = `${l} ${worker}\t[${contexts.join(' ')}]\t${m}`;
|
2021-10-08 14:24:05 +02:00
|
|
|
if (envOption.withLogTime) log = chalk.gray(time) + ' ' + log;
|
2019-03-02 10:51:59 +01:00
|
|
|
|
2024-01-02 08:52:51 +01:00
|
|
|
const args: unknown[] = [important ? chalk.bold(log) : log];
|
|
|
|
if (data != null) {
|
2024-01-15 18:29:53 +01:00
|
|
|
args.push(JSON.stringify(data, inspect, 2));
|
2024-01-02 08:52:51 +01:00
|
|
|
}
|
2024-01-11 03:41:20 +01:00
|
|
|
|
|
|
|
if (level === 'error' || level === 'warning') {
|
|
|
|
console.error(...args);
|
|
|
|
} else {
|
|
|
|
console.log(...args);
|
|
|
|
}
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2019-04-12 18:43:22 +02:00
|
|
|
public error(x: string | Error, data?: Record<string, any> | null, important = false): void { // 実行を継続できない状況で使う
|
2019-03-02 10:51:59 +01:00
|
|
|
if (x instanceof Error) {
|
2022-09-17 20:27:08 +02:00
|
|
|
data = data ?? {};
|
2024-01-11 03:41:20 +01:00
|
|
|
data.error = x;
|
|
|
|
|
2019-03-02 10:51:59 +01:00
|
|
|
this.log('error', x.toString(), data, important);
|
2021-02-27 09:39:55 +01:00
|
|
|
} else if (typeof x === 'object') {
|
2024-01-11 03:41:20 +01:00
|
|
|
data = data ?? {};
|
|
|
|
data.error = data.error ?? x;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
this.log('error', `${(x as any).message ?? (x as any).name ?? x}`, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
} else {
|
2021-02-27 09:39:55 +01:00
|
|
|
this.log('error', `${x}`, data, important);
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-01-11 03:41:20 +01:00
|
|
|
public warn(x: string | Error, data?: Record<string, any> | null, important = false): void { // 実行を継続できるが改善すべき状況で使う
|
|
|
|
if (x instanceof Error) {
|
|
|
|
data = data ?? {};
|
|
|
|
data.error = x;
|
|
|
|
|
|
|
|
this.log('warning', x.toString(), data, important);
|
|
|
|
} else if (typeof x === 'object') {
|
|
|
|
data = data ?? {};
|
|
|
|
data.error = data.error ?? x;
|
|
|
|
|
|
|
|
this.log('warning', `${(x as any).message ?? (x as any).name ?? x}`, data, important);
|
|
|
|
} else {
|
|
|
|
this.log('warning', `${x}`, data, important);
|
|
|
|
}
|
2019-03-02 10:51:59 +01:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2019-04-12 18:43:22 +02:00
|
|
|
public succ(message: string, data?: Record<string, any> | null, important = false): void { // 何かに成功した状況で使う
|
2019-03-02 10:51:59 +01:00
|
|
|
this.log('success', message, data, important);
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2019-04-12 18:43:22 +02:00
|
|
|
public debug(message: string, data?: Record<string, any> | null, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
2022-02-04 02:41:27 +01:00
|
|
|
if (process.env.NODE_ENV !== 'production' || envOption.verbose) {
|
2019-03-02 10:51:59 +01:00
|
|
|
this.log('debug', message, data, important);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2019-04-12 18:43:22 +02:00
|
|
|
public info(message: string, data?: Record<string, any> | null, important = false): void { // それ以外
|
2019-03-02 10:51:59 +01:00
|
|
|
this.log('info', message, data, important);
|
|
|
|
}
|
|
|
|
}
|