| | |
| | | type Level = 'LOG' | 'DEBUG' | 'INFO' | 'WARN' | 'ERROR' | 'FATAL'; |
| | | |
| | | class Logger { |
| | | static level = 'LOG'; // 默认为LOG级别 |
| | | static logPaletteColor = { |
| | | private static level = 'LOG'; // 默认为LOG级别 |
| | | private static logPaletteColor = { |
| | | default: '#78909C', |
| | | LOG: '#78909C', |
| | | INFO: '#5F9FD7', |
| | |
| | | TIME: '⏰', |
| | | }; |
| | | |
| | | static getLogPaletteCss(level: Level) { |
| | | private static getLogPaletteCss(level: Level) { |
| | | return `background-color: ${this.logPaletteColor[level] || this.logPaletteColor['default']}; color: #fff; padding: 2px 4px; border-radius: 4px;}; margin-right: 10px;`; |
| | | } |
| | | |
| | |
| | | } |
| | | |
| | | // 根据日志等级判断是否应该打印日志 |
| | | static shouldLog(level: Level) { |
| | | private static shouldLog(level: Level) { |
| | | const levels = ['LOG', 'DEBUG', 'INFO', 'WARN', 'ERROR', 'FATAL']; |
| | | return levels.indexOf(level) >= levels.indexOf(this.level); |
| | | } |
| | | |
| | | static formatStack(stack: string | undefined) { |
| | | private static formatStack(stack: string | undefined) { |
| | | if (!stack) return ''; |
| | | // 格式化错误堆栈的逻辑 |
| | | return stack |
| | |
| | | .join('\n'); |
| | | } |
| | | |
| | | static logger(level: Level, message: string | object, error?: Error) { |
| | | private static logger(level: Level, message: string | object, error?: Error) { |
| | | if (!this.shouldLog(level)) { |
| | | return; |
| | | } |
| | | // const timestamp = new Date().toISOString(); |
| | | const timestamp = dayjs(); |
| | | const timestamp = dayjs().format('YYYY-MM-DD HH:mm:ss SSS'); |
| | | const stack = error ? error.stack : ''; |
| | | |
| | | let formattedMessage = `%c${level}%c${this.logEmoji['TIME']}${timestamp}${this.logEmoji['TIME']}%c${stack}`; |