Better logger
This commit is contained in:
		| @@ -25,9 +25,9 @@ import { Config } from './config/types'; | |||||||
| import { lessThan } from './prelude/array'; | import { lessThan } from './prelude/array'; | ||||||
| import * as pkg from '../package.json'; | import * as pkg from '../package.json'; | ||||||
|  |  | ||||||
| const logger = new Logger('core'); | const logger = new Logger('core', 'cyan'); | ||||||
| const bootLogger = logger.createSubLogger('boot'); | const bootLogger = logger.createSubLogger('boot', 'magenta'); | ||||||
| const clusterLog = logger.createSubLogger('cluster'); | const clusterLog = logger.createSubLogger('cluster', 'orange'); | ||||||
| const ev = new Xev(); | const ev = new Xev(); | ||||||
|  |  | ||||||
| if (process.env.NODE_ENV != 'production' && process.env.DEBUG == null) { | if (process.env.NODE_ENV != 'production' && process.env.DEBUG == null) { | ||||||
|   | |||||||
| @@ -3,24 +3,27 @@ import * as dateformat from 'dateformat'; | |||||||
|  |  | ||||||
| export default class Logger { | export default class Logger { | ||||||
| 	private domain: string; | 	private domain: string; | ||||||
|  | 	private color?: string; | ||||||
| 	private parentLogger: Logger; | 	private parentLogger: Logger; | ||||||
|  |  | ||||||
| 	constructor(domain: string) { | 	constructor(domain: string, color?: string) { | ||||||
| 		this.domain = domain; | 		this.domain = domain; | ||||||
|  | 		this.color = color; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public createSubLogger(domain: string): Logger { | 	public createSubLogger(domain: string, color?: string): Logger { | ||||||
| 		const logger = new Logger(domain); | 		const logger = new Logger(domain, color); | ||||||
| 		logger.parentLogger = this; | 		logger.parentLogger = this; | ||||||
| 		return logger; | 		return logger; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	public log(level: string, message: string, important = false): void { | 	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) { | 		if (this.parentLogger) { | ||||||
| 			this.parentLogger.log(level, `[${this.domain}]\t${message}`, important); | 			this.parentLogger.log(level, `[${domain}]\t${message}`, important); | ||||||
| 		} else { | 		} else { | ||||||
| 			const time = dateformat(new Date(), 'HH:MM:ss'); | 			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); | 			console.log(important ? chalk.bold(log) : log); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 syuilo
					syuilo