1
0
mirror of https://github.com/MisskeyIO/misskey synced 2025-01-07 10:23:43 +09:00
MisskeyIO/src/misc/logger.ts

48 lines
1.5 KiB
TypeScript
Raw Normal View History

2018-07-14 20:58:21 +09:00
import chalk from 'chalk';
import * as dateformat from 'dateformat';
2016-12-29 23:09:21 +09:00
export default class Logger {
2017-05-24 20:50:17 +09:00
private domain: string;
2019-02-03 01:39:42 +09:00
private color?: string;
2019-02-03 01:01:40 +09:00
private parentLogger: Logger;
2016-12-29 23:09:21 +09:00
2019-02-03 01:39:42 +09:00
constructor(domain: string, color?: string) {
2017-05-24 20:50:17 +09:00
this.domain = domain;
2019-02-03 01:39:42 +09:00
this.color = color;
2019-02-03 01:24:59 +09:00
}
2019-02-03 01:39:42 +09:00
public createSubLogger(domain: string, color?: string): Logger {
const logger = new Logger(domain, color);
2019-02-03 01:24:59 +09:00
logger.parentLogger = this;
return logger;
2017-05-24 20:50:17 +09:00
}
2019-02-03 01:20:21 +09:00
public log(level: string, message: string, important = false): void {
2019-02-03 01:39:42 +09:00
const domain = this.color ? chalk.keyword(this.color)(this.domain) : chalk.white(this.domain);
2019-02-03 01:01:40 +09:00
if (this.parentLogger) {
2019-02-03 01:39:42 +09:00
this.parentLogger.log(level, `[${domain}]\t${message}`, important);
2019-02-03 01:01:40 +09:00
} else {
const time = dateformat(new Date(), 'HH:MM:ss');
2019-02-03 01:39:42 +09:00
const log = `${chalk.gray(time)} ${level} [${domain}]\t${message}`;
2019-02-03 01:20:21 +09:00
console.log(important ? chalk.bold(log) : log);
2019-02-03 01:01:40 +09:00
}
2016-12-29 20:03:34 +09:00
}
2016-12-29 23:09:21 +09:00
2019-02-03 01:20:21 +09:00
public error(message: string | Error): void { // 実行を継続できない状況で使う
this.log(chalk.red.bold('ERROR'), chalk.red.bold(message.toString()));
2016-12-29 23:09:21 +09:00
}
2019-02-03 01:33:34 +09:00
public warn(message: string, important = false): void { // 実行を継続できるが改善すべき状況で使う
this.log(chalk.yellow.bold('WARN'), chalk.yellow.bold(message), important);
2016-12-29 23:09:21 +09:00
}
2019-02-03 01:20:21 +09:00
public succ(message: string, important = false): void { // 何かに成功した状況で使う
this.log(chalk.blue.green('DONE'), chalk.green.bold(message), important);
2018-07-14 20:58:21 +09:00
}
2019-02-03 01:33:34 +09:00
public info(message: string, important = false): void { // それ以外
this.log(chalk.blue.bold('INFO'), message, important);
2016-12-29 23:09:21 +09:00
}
2018-07-14 22:13:42 +09:00
}