2019-03-02 09:51:59 +00:00
|
|
|
import * as cluster from 'cluster';
|
|
|
|
import * as os from 'os';
|
2019-11-24 08:09:32 +00:00
|
|
|
import * as chalk from 'chalk';
|
2019-03-02 09:51:59 +00:00
|
|
|
import * as dateformat from 'dateformat';
|
|
|
|
import { program } from '../argv';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { getRepository } from 'typeorm';
|
|
|
|
import { Log } from '../models/entities/log';
|
|
|
|
import { genId } from '../misc/gen-id';
|
2019-08-29 23:29:46 +00:00
|
|
|
import config from '../config';
|
|
|
|
|
|
|
|
const SyslogPro = require('syslog-pro');
|
2019-03-02 09:51:59 +00:00
|
|
|
|
|
|
|
type Domain = {
|
|
|
|
name: string;
|
2019-04-12 16:43:22 +00:00
|
|
|
color?: string;
|
2019-03-02 09:51:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
type Level = 'error' | 'success' | 'warning' | 'debug' | 'info';
|
|
|
|
|
|
|
|
export default class Logger {
|
|
|
|
private domain: Domain;
|
2019-04-12 16:43:22 +00:00
|
|
|
private parentLogger: Logger | null = null;
|
2019-03-02 09:51:59 +00:00
|
|
|
private store: boolean;
|
2019-08-29 23:29:46 +00:00
|
|
|
private syslogClient: any | null = null;
|
2019-03-02 09:51:59 +00:00
|
|
|
|
|
|
|
constructor(domain: string, color?: string, store = true) {
|
|
|
|
this.domain = {
|
|
|
|
name: domain,
|
|
|
|
color: color,
|
|
|
|
};
|
|
|
|
this.store = store;
|
2019-08-29 23:29:46 +00:00
|
|
|
|
|
|
|
if (config.syslog) {
|
|
|
|
this.syslogClient = new SyslogPro.RFC5424({
|
|
|
|
applacationName: 'Misskey',
|
|
|
|
timestamp: true,
|
|
|
|
encludeStructuredData: true,
|
|
|
|
color: true,
|
|
|
|
extendedColor: true,
|
|
|
|
server: {
|
|
|
|
target: config.syslog.host,
|
|
|
|
port: config.syslog.port,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public createSubLogger(domain: string, color?: string, store = true): Logger {
|
|
|
|
const logger = new Logger(domain, color, store);
|
|
|
|
logger.parentLogger = this;
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
private log(level: Level, message: string, data?: Record<string, any> | null, important = false, subDomains: Domain[] = [], store = true): void {
|
2019-03-02 09:51:59 +00:00
|
|
|
if (program.quiet) return;
|
|
|
|
if (!this.store) store = false;
|
2020-03-28 09:28:21 +00:00
|
|
|
if (level === 'debug') store = false;
|
2019-03-02 09:51:59 +00:00
|
|
|
|
|
|
|
if (this.parentLogger) {
|
|
|
|
this.parentLogger.log(level, message, data, important, [this.domain].concat(subDomains), store);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const time = dateformat(new Date(), 'HH:MM:ss');
|
|
|
|
const worker = cluster.isMaster ? '*' : cluster.worker.id;
|
|
|
|
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;
|
|
|
|
const domains = [this.domain].concat(subDomains).map(d => d.color ? chalk.keyword(d.color)(d.name) : chalk.white(d.name));
|
|
|
|
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;
|
|
|
|
|
|
|
|
let log = `${l} ${worker}\t[${domains.join(' ')}]\t${m}`;
|
|
|
|
if (program.withLogTime) log = chalk.gray(time) + ' ' + log;
|
|
|
|
|
|
|
|
console.log(important ? chalk.bold(log) : log);
|
|
|
|
|
|
|
|
if (store) {
|
2019-08-29 23:29:46 +00: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;
|
|
|
|
|
2019-12-11 15:39:59 +00:00
|
|
|
send.bind(this.syslogClient)(message);
|
2019-08-29 23:29:46 +00:00
|
|
|
} else {
|
|
|
|
const Logs = getRepository(Log);
|
|
|
|
Logs.insert({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
machine: os.hostname(),
|
|
|
|
worker: worker.toString(),
|
|
|
|
domain: [this.domain].concat(subDomains).map(d => d.name),
|
|
|
|
level: level,
|
2019-11-24 07:43:19 +00:00
|
|
|
message: message.substr(0, 1000), // 1024を超えるとログが挿入できずエラーになり無限ループする
|
2019-08-29 23:29:46 +00:00
|
|
|
data: data,
|
|
|
|
} as Log);
|
|
|
|
}
|
2019-03-02 09:51:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
public error(x: string | Error, data?: Record<string, any> | null, important = false): void { // 実行を継続できない状況で使う
|
2019-03-02 09:51:59 +00:00
|
|
|
if (x instanceof Error) {
|
|
|
|
data = data || {};
|
|
|
|
data.e = x;
|
|
|
|
this.log('error', x.toString(), data, important);
|
|
|
|
} else {
|
|
|
|
this.log('error', x, data, important);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 13:54:19 +00:00
|
|
|
public warn(message: string, data?: Record<string, any> | null, important = false): void { // 実行を継続できるが改善すべき状況で使う
|
2019-03-02 09:51:59 +00:00
|
|
|
this.log('warning', message, data, important);
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
public succ(message: string, data?: Record<string, any> | null, important = false): void { // 何かに成功した状況で使う
|
2019-03-02 09:51:59 +00:00
|
|
|
this.log('success', message, data, important);
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
public debug(message: string, data?: Record<string, any> | null, important = false): void { // デバッグ用に使う(開発者に必要だが利用者に不要な情報)
|
2019-03-02 09:51:59 +00:00
|
|
|
if (process.env.NODE_ENV != 'production' || program.verbose) {
|
|
|
|
this.log('debug', message, data, important);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 16:43:22 +00:00
|
|
|
public info(message: string, data?: Record<string, any> | null, important = false): void { // それ以外
|
2019-03-02 09:51:59 +00:00
|
|
|
this.log('info', message, data, important);
|
|
|
|
}
|
|
|
|
}
|