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