All files / dingtalk-openclaw-connector/src/utils logger.ts

5% Statements 2/40
100% Branches 0/0
0% Functions 0/2
5% Lines 2/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                      1x                                                                                                                               1x      
/**
 * 日志工具模块
 * 根据 debug 配置控制日志输出
 */
 
/**
 * 创建日志记录器
 * @param debug - 是否启用 debug 模式
 * @param prefix - 日志前缀
 * @returns 日志记录器对象
 */
export function createLogger(debug: boolean = false, prefix: string = '') {
  const logger = {
    /**
     * 打印 info 级别日志
     * 仅在 debug 模式下输出
     */
    info(...args: any[]) {
      if (debug) {
        if (prefix) {
          console.log(`[${prefix}]`, ...args);
        } else {
          console.log(...args);
        }
      }
    },
 
    /**
     * 打印 warn 级别日志
     * 始终输出
     */
    warn(...args: any[]) {
      if (prefix) {
        console.warn(`[${prefix}]`, ...args);
      } else {
        console.warn(...args);
      }
    },
 
    /**
     * 打印 error 级别日志
     * 始终输出
     */
    error(...args: any[]) {
      if (prefix) {
        console.error(`[${prefix}]`, ...args);
      } else {
        console.error(...args);
      }
    },
 
    /**
     * 打印 debug 级别日志
     * 仅在 debug 模式下输出
     */
    debug(...args: any[]) {
      if (debug) {
        if (prefix) {
          console.log(`[DEBUG][${prefix}]`, ...args);
        } else {
          console.log('[DEBUG]', ...args);
        }
      }
    },
  };
 
  return logger;
}
 
/**
 * 从配置中创建日志记录器
 * @param config - 包含 debug 配置的对象
 * @param prefix - 日志前缀
 * @returns 日志记录器对象
 */
export function createLoggerFromConfig(config: { debug?: boolean }, prefix: string = '') {
  return createLogger(!!config.debug, prefix);
}