Refatoring of logger

This commit is contained in:
syuilo
2019-02-03 01:01:40 +09:00
parent cefecd7903
commit 52d6ec2138
2 changed files with 34 additions and 30 deletions

View File

@ -3,14 +3,20 @@ import * as dateformat from 'dateformat';
export default class Logger {
private domain: string;
private parentLogger: Logger;
constructor(domain: string) {
constructor(domain: string, parentLogger?: Logger) {
this.domain = domain;
this.parentLogger = parentLogger;
}
public static log(level: string, message: string): void {
const time = dateformat(new Date(), 'HH:MM:ss');
console.log(`[${time} ${level}] ${message}`);
public log(level: string, message: string): void {
if (this.parentLogger) {
this.parentLogger.log(level, `[${this.domain}] ${message}`);
} else {
const time = dateformat(new Date(), 'HH:MM:ss');
console.log(`${chalk.gray(time)} ${level} [${this.domain}] ${message}`);
}
}
public static error(message: string): void {
@ -29,11 +35,6 @@ export default class Logger {
(new Logger('')).info(message);
}
public log(level: string, message: string) {
const domain = this.domain.length > 0 ? `[${this.domain}] ` : '';
Logger.log(level, `${domain}${message}`);
}
public error(message: string): void { // 実行を継続できない状況で使う
this.log(chalk.red.bold('ERROR'), chalk.red.bold(message));
}