summaryrefslogtreecommitdiff
path: root/src/utils/logger.ts
blob: ecfacbc952dad74706e0270727353eebc1438d68 (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
45
46
47
48
49
50
51
52
53
import * as chalk from 'chalk';

export type LogLevel = 'Error' | 'Warn' | 'Info';

function toLevelColor(level: LogLevel): chalk.ChalkStyle {
	switch (level) {
		case 'Error': return chalk.red;
		case 'Warn': return chalk.yellow;
		case 'Info': return chalk.blue;
	}
}

export default class Logger {
	private domain: string;

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

	public static log(level: LogLevel, message: string): void {
		const color = toLevelColor(level);
		const time = (new Date()).toLocaleTimeString('ja-JP');
		console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${message}`);
	}

	public static error(message: string): void {
		Logger.log('Error', message);
	}

	public static warn(message: string): void {
		Logger.log('Warn', message);
	}

	public static info(message: string): void {
		Logger.log('Info', message);
	}

	public log(level: LogLevel, message: string): void {
		Logger.log(level, `[${this.domain}] ${message}`);
	}

	public error(message: string): void {
		this.log('Error', message);
	}

	public warn(message: string): void {
		this.log('Warn', message);
	}

	public info(message: string): void {
		this.log('Info', message);
	}
}