Sharkey/src/misc/logger.ts

55 lines
1.4 KiB
TypeScript
Raw Normal View History

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