2019-02-03 05:51:24 +01:00
|
|
|
|
import * as cluster from 'cluster';
|
2018-07-14 13:58:21 +02:00
|
|
|
|
import chalk from 'chalk';
|
|
|
|
|
import * as dateformat from 'dateformat';
|
2019-02-03 12:10:20 +01:00
|
|
|
|
import { program } from '../argv';
|
2019-02-03 10:16:57 +01:00
|
|
|
|
|
2016-12-29 15:09:21 +01:00
|
|
|
|
export default class Logger {
|
2017-05-24 13:50:17 +02:00
|
|
|
|
private domain: string;
|
2019-02-02 17:39:42 +01:00
|
|
|
|
private color?: string;
|
2019-02-02 17:01:40 +01:00
|
|
|
|
private parentLogger: Logger;
|
2016-12-29 15:09:21 +01:00
|
|
|
|
|
2019-02-02 17:39:42 +01:00
|
|
|
|
constructor(domain: string, color?: string) {
|
2017-05-24 13:50:17 +02:00
|
|
|
|
this.domain = domain;
|
2019-02-02 17:39:42 +01:00
|
|
|
|
this.color = color;
|
2019-02-02 17:24:59 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 17:39:42 +01:00
|
|
|
|
public createSubLogger(domain: string, color?: string): Logger {
|
|
|
|
|
const logger = new Logger(domain, color);
|
2019-02-02 17:24:59 +01:00
|
|
|
|
logger.parentLogger = this;
|
|
|
|
|
return logger;
|
2017-05-24 13:50:17 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 03:51:23 +01:00
|
|
|
|
private log(level: string, message: string, important = false, subDomains: string[] = []): void {
|
2019-02-03 12:10:20 +01:00
|
|
|
|
if (program.quiet) return;
|
2019-02-02 17:39:42 +01:00
|
|
|
|
const domain = this.color ? chalk.keyword(this.color)(this.domain) : chalk.white(this.domain);
|
2019-02-03 13:42:52 +01:00
|
|
|
|
const domains = [domain].concat(subDomains);
|
2019-02-02 17:01:40 +01:00
|
|
|
|
if (this.parentLogger) {
|
2019-02-03 13:42:52 +01:00
|
|
|
|
this.parentLogger.log(level, message, important, domains);
|
2019-02-02 17:01:40 +01:00
|
|
|
|
} else {
|
|
|
|
|
const time = dateformat(new Date(), 'HH:MM:ss');
|
2019-02-03 05:51:24 +01:00
|
|
|
|
const process = cluster.isMaster ? '*' : cluster.worker.id;
|
2019-02-03 13:42:52 +01:00
|
|
|
|
const log = `${chalk.gray(time)} ${level} ${process}\t[${domains.join(' ')}]\t${message}`;
|
2019-02-02 17:20:21 +01:00
|
|
|
|
console.log(important ? chalk.bold(log) : log);
|
2019-02-02 17:01:40 +01:00
|
|
|
|
}
|
2016-12-29 12:03:34 +01:00
|
|
|
|
}
|
2016-12-29 15:09:21 +01:00
|
|
|
|
|
2019-02-03 10:16:57 +01:00
|
|
|
|
public error(message: string | Error, important = false): void { // 実行を継続できない状況で使う
|
2019-02-03 12:23:53 +01:00
|
|
|
|
this.log(important ? chalk.bgRed.white('ERR ') : chalk.red('ERR '), chalk.red(message.toString()), important);
|
2016-12-29 15:09:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 17:33:34 +01:00
|
|
|
|
public warn(message: string, important = false): void { // 実行を継続できるが改善すべき状況で使う
|
2019-02-03 08:45:13 +01:00
|
|
|
|
this.log(chalk.yellow('WARN'), chalk.yellow(message), important);
|
2016-12-29 15:09:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-02 17:20:21 +01:00
|
|
|
|
public succ(message: string, important = false): void { // 何かに成功した状況で使う
|
2019-02-03 12:23:53 +01:00
|
|
|
|
this.log(important ? chalk.bgGreen.white('DONE') : chalk.green('DONE'), chalk.green(message), important);
|
2018-07-14 13:58:21 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 04:09:59 +01:00
|
|
|
|
public debug(message: string, important = false): void { // デバッグ用に使う(開発者にとっては必要だが利用者にとっては不要な情報)
|
2019-02-03 08:45:13 +01:00
|
|
|
|
if (process.env.NODE_ENV != 'production') {
|
|
|
|
|
this.log(chalk.gray('VERB'), chalk.gray(message), important);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-02-04 04:09:59 +01:00
|
|
|
|
|
|
|
|
|
public info(message: string, important = false): void { // それ以外
|
|
|
|
|
this.log(chalk.blue('INFO'), message, important);
|
|
|
|
|
}
|
2016-12-29 09:36:03 +01:00
|
|
|
|
}
|