summaryrefslogtreecommitdiff
path: root/src/utils/logger.ts
blob: 2189dd5ea52782b130cd0f5d4dd3b34449ea117f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 function log(level: LogLevel, message: string): void;
export function log(level: LogLevel, message: string, domain: string): void;
export function log(level: LogLevel, message: string, domain?: string): void {
	if (typeof domain == 'string') {
		log(level, `[${domain}] ${message}`);
	} else {
		let color = toLevelColor(level);
		let time = (new Date()).toLocaleTimeString('ja-JP');
		console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${message}`);
	}
}