class LOG_LEVEL_TYPES { public static DEBUG = 0 public static LOG = 1 public static INFO = 2 public static WARN = 3 public static ERROR = 4 } const Log_Level_Names: Array = ['debug', 'log', 'info', 'warn', 'error'] export class Logger { public static tag: string = '[HaoJslog]' // 可以设置当前游戏的前缀 public static LEVEL: number = LOG_LEVEL_TYPES.WARN // 当前Logger等级 public static Log_Color_Config: Array = [ 'color:#890;font-size:10px;', 'color:#000;font-size:11px;', 'color:#09f;font-size:12px;', 'color:#f90;font-size:13px;', 'color:#f00;font-size:15px;', ] private static Terminal_Log: boolean = false public static formatNow() { const date: Date = new Date() // 后端返回的时间戳是秒 return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}:${date.getMilliseconds()}` } private static getLogPreKey(nowLevel: number): string { return `[${Logger.formatNow()}] ${Logger.tag} [${Log_Level_Names[nowLevel]}] ` } public static debug(...params: any) { if (Logger.LEVEL > LOG_LEVEL_TYPES.DEBUG) return const str: string = this.getLogPreKey(LOG_LEVEL_TYPES.DEBUG) const fileStr: string = str + params.join(' ') // LogErrorFileUtil.debug(fileStr); if (this.Terminal_Log) console.log(`%c${str}`, this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG], ...params) else console.info(fileStr) } public static log(...params: any) { if (Logger.LEVEL > LOG_LEVEL_TYPES.LOG) return const str: string = this.getLogPreKey(LOG_LEVEL_TYPES.LOG) const fileStr: string = str + params.join(' ') // LogErrorFileUtil.log(fileStr); if (this.Terminal_Log) console.log(`%c${str}`, this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG], ...params) else console.info(fileStr) // console.log(str, ...params) } public static info(...params: any) { if (Logger.LEVEL > LOG_LEVEL_TYPES.INFO) return const str: string = this.getLogPreKey(LOG_LEVEL_TYPES.INFO) const fileStr: string = str + params.join(' ') if (this.Terminal_Log) console.info(`%c${str}`, this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG], ...params) else console.info(fileStr) } public static warn(...params: any) { if (Logger.LEVEL > LOG_LEVEL_TYPES.WARN) return const str: string = this.getLogPreKey(LOG_LEVEL_TYPES.WARN) const fileStr: string = str + params.join(' ') if (this.Terminal_Log) console.warn(`%c${str}`, this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG], ...params) else console.warn(fileStr) } public static error(...params: any) { if (Logger.LEVEL > LOG_LEVEL_TYPES.ERROR) return const str: string = this.getLogPreKey(LOG_LEVEL_TYPES.ERROR) const fileStr: string = str + params.join(' ') if (this.Terminal_Log) console.error(`%c${str}`, this.Log_Color_Config[LOG_LEVEL_TYPES.DEBUG], ...params) else console.error(fileStr) } }