From f53a93ea137b3a184f91f9a0e72a8c5eb1e790fc Mon Sep 17 00:00:00 2001 From: syuilo Date: Sun, 3 Feb 2019 01:39:42 +0900 Subject: [PATCH] Better logger --- src/index.ts | 6 +++--- src/misc/logger.ts | 13 ++++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 8ec8a2744b..ab0ca1ce7d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -25,9 +25,9 @@ import { Config } from './config/types'; import { lessThan } from './prelude/array'; import * as pkg from '../package.json'; -const logger = new Logger('core'); -const bootLogger = logger.createSubLogger('boot'); -const clusterLog = logger.createSubLogger('cluster'); +const logger = new Logger('core', 'cyan'); +const bootLogger = logger.createSubLogger('boot', 'magenta'); +const clusterLog = logger.createSubLogger('cluster', 'orange'); const ev = new Xev(); if (process.env.NODE_ENV != 'production' && process.env.DEBUG == null) { diff --git a/src/misc/logger.ts b/src/misc/logger.ts index 2ca79dd4b6..bfdb48c646 100644 --- a/src/misc/logger.ts +++ b/src/misc/logger.ts @@ -3,24 +3,27 @@ import * as dateformat from 'dateformat'; export default class Logger { private domain: string; + private color?: string; private parentLogger: Logger; - constructor(domain: string) { + constructor(domain: string, color?: string) { this.domain = domain; + this.color = color; } - public createSubLogger(domain: string): Logger { - const logger = new Logger(domain); + public createSubLogger(domain: string, color?: string): Logger { + const logger = new Logger(domain, color); logger.parentLogger = this; return logger; } public log(level: string, message: string, important = false): void { + const domain = this.color ? chalk.keyword(this.color)(this.domain) : chalk.white(this.domain); if (this.parentLogger) { - this.parentLogger.log(level, `[${this.domain}]\t${message}`, important); + this.parentLogger.log(level, `[${domain}]\t${message}`, important); } else { const time = dateformat(new Date(), 'HH:MM:ss'); - const log = `${chalk.gray(time)} ${level} [${this.domain}]\t${message}`; + const log = `${chalk.gray(time)} ${level} [${domain}]\t${message}`; console.log(important ? chalk.bold(log) : log); } }