Sanakey
2024-08-07 807478b46d3940ba0fb5353f7cb8d220bc67bbf9
src/utils/logger.ts
@@ -15,8 +15,8 @@
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',
@@ -35,7 +35,7 @@
    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;`;
  }
@@ -44,12 +44,12 @@
  }
  // 根据日志等级判断是否应该打印日志
  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
@@ -58,12 +58,12 @@
      .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}`;