summaryrefslogtreecommitdiff
path: root/src/misc/logger.ts
blob: 2ca79dd4b6cb7cb82ff34776812e6ed09bffee56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import chalk from 'chalk';
import * as dateformat from 'dateformat';

export default class Logger {
	private domain: string;
	private parentLogger: Logger;

	constructor(domain: string) {
		this.domain = domain;
	}

	public createSubLogger(domain: string): Logger {
		const logger = new Logger(domain);
		logger.parentLogger = this;
		return logger;
	}

	public log(level: string, message: string, important = false): void {
		if (this.parentLogger) {
			this.parentLogger.log(level, `[${this.domain}]\t${message}`, important);
		} else {
			const time = dateformat(new Date(), 'HH:MM:ss');
			const log = `${chalk.gray(time)} ${level} [${this.domain}]\t${message}`;
			console.log(important ? chalk.bold(log) : log);
		}
	}

	public error(message: string | Error): void { // 実行を継続できない状況で使う
		this.log(chalk.red.bold('ERROR'), chalk.red.bold(message.toString()));
	}

	public warn(message: string, important = false): void { // 実行を継続できるが改善すべき状況で使う
		this.log(chalk.yellow.bold('WARN'), chalk.yellow.bold(message), important);
	}

	public succ(message: string, important = false): void { // 何かに成功した状況で使う
		this.log(chalk.blue.green('DONE'), chalk.green.bold(message), important);
	}

	public info(message: string, important = false): void { // それ以外
		this.log(chalk.blue.bold('INFO'), message, important);
	}

}