import chalk from 'chalk' import loading from 'loading-cli' /** * 指示日志消息的严重性。 * 日志级别按严重性递增的顺序排列。所以“Debug”比“Trace”等更严重。 * */ export enum LogLevel { /** 成功. */ Success = 'success', /** 警告. */ Warning = 'warning', /** 错误. */ Error = 'error', /** 调试信息 */ Debug = 'debug', /** 消息 */ Info = 'info' } /** 日志工具 */ export default class Logger { /** loading 工具 */ private load: loading.Loading loading = { start: (text: string = '正在加载...', color: string = 'yellow') => { if (this.load) this.load.stop() this.load = loading({ text: chalk.cyan(`[ys-cli]`) + ` ${text}`, color, interval: 50, frames: ['◐', '◓', '◑', '◒'] }).start() }, stop: () => { this.load.stop() delete this.load }, update: (text: string, color?: string) => { if (!text) return this.load.text = text if (color) this.load.color = color } } private getLogColor(logLevel: LogLevel) { switch (logLevel) { case LogLevel.Error: return '#ed4014' case LogLevel.Warning: return '#ff9900' case LogLevel.Info: return '#76aad8' case LogLevel.Success: return '#19be6b' case LogLevel.Debug: default: return '#ffa940' } } /** point log */ private log(logLevel: LogLevel, ...msg: any): void { console.log( chalk.cyan(`[ys-cli]`), chalk.hex('#787778')(`(${chalk.hex(this.getLogColor(logLevel)).underline(logLevel)})`), chalk.hex('#d3d3d3')(...msg) ) } success(...msg: any) { this.log(LogLevel.Success, ...msg) } error(...msg: any) { this.log(LogLevel.Error, ...msg) } warn(...msg: any) { this.log(LogLevel.Warning, ...msg) } debug(...msg: any) { this.log(LogLevel.Debug, ...msg) } info(...msg: any) { this.log(LogLevel.Info, ...msg) } } export const logger: Logger = new Logger()