From aa4ef6745ad798bd7d4f05cb397ef1dd85279814 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 7 Jul 2018 19:19:00 +0900 Subject: Refactorng --- src/misc/logger.ts | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/misc/logger.ts (limited to 'src/misc/logger.ts') diff --git a/src/misc/logger.ts b/src/misc/logger.ts new file mode 100644 index 0000000000..fae1042c39 --- /dev/null +++ b/src/misc/logger.ts @@ -0,0 +1,53 @@ +import chalk, { Chalk } from 'chalk'; + +export type LogLevel = 'Error' | 'Warn' | 'Info'; + +function toLevelColor(level: LogLevel): Chalk { + 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); + } +} -- cgit v1.2.3-freya