2016-12-29 02:36:03 -06:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 08:09:21 -06:00
|
|
|
export default class Logger {
|
2017-05-24 06:50:17 -05:00
|
|
|
private domain: string;
|
2016-12-29 08:09:21 -06:00
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
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');
|
2016-12-29 05:03:34 -06:00
|
|
|
console.log(`[${time} ${color.bold(level.toUpperCase())}]: ${message}`);
|
|
|
|
}
|
2016-12-29 08:09:21 -06:00
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public static error(message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
Logger.log('Error', message);
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public static warn(message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
Logger.log('Warn', message);
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public static info(message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
Logger.log('Info', message);
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public log(level: LogLevel, message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
Logger.log(level, `[${this.domain}] ${message}`);
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public error(message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
this.log('Error', message);
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public warn(message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
this.log('Warn', message);
|
|
|
|
}
|
|
|
|
|
2017-05-24 06:50:17 -05:00
|
|
|
public info(message: string): void {
|
2016-12-29 08:09:21 -06:00
|
|
|
this.log('Info', message);
|
|
|
|
}
|
2016-12-29 02:36:03 -06:00
|
|
|
}
|