All files / src/logger index.ts

96.88% Statements 31/32
83.33% Branches 10/12
100% Functions 6/6
95.83% Lines 23/24

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 621x       1x 1x       9x     9x 9x 9x               1x 2x 1x               12x 4x 4x 1x               12x 4x 4x 2x               12x 4x 4x 3x     1x  
import { reportV3, V3LogInfo } from './report-v3'
 
export type LogLevel = 'INFO' | 'WARN' | 'ERROR' | 'OFF'
 
export default class Logger {
  private static id: number = 0
 
  // 为每个类分配一个 id
  // 用以区分不同的上传任务
  private id = ++Logger.id
 
  constructor(
    private token: string,
    private IdisableReport = true,
    private Ilevel: LogLevel = 'OFF'
  ) { }
 
  /**
   * @param  {V3LogInfo} data 上报的数据。
   * @param  {boolean} retry 重试次数,可选,默认为 3。
   * @description 向服务端上报统计信息。
   */
  report(data: V3LogInfo, retry?: number) {
    if (this.disableReport) return
    try { reportV3(this.token, data, retry) }
    catch (error) { console.warn(error) }
  }
 
  /**
   * @param  {unknown[]} ...args
   * @description 输出 info 级别的调试信息。
   */
  info(...args: unknown[]) {
    const allowLevel: LogLevel[] = ['INFO']
    if (allowLevel.includes(this.level)) {
      console.log(`Qiniu-JS-SDK [INFO][${this.id}]: `, ...args)
    }
  }
 
  /**
   * @param  {unknown[]} ...args
   * @description 输出 warn 级别的调试信息。
   */
  warn(...args: unknown[]) {
    const allowLevel: LogLevel[] = ['INFO', 'WARN']
    if (allowLevel.includes(this.level)) {
      console.warn(`Qiniu-JS-SDK [WARN][${this.id}]: `, ...args)
    }
  }
 
  /**
   * @param  {unknown[]} ...args
   * @description 输出 error 级别的调试信息。
   */
  error(...args: unknown[]) {
    const allowLevel: LogLevel[] = ['INFO', 'WARN', 'ERROR']
    if (allowLevel.includes(this.level)) {
      console.error(`Qiniu-JS-SDK [ERROR][${this.id}]: `, ...args)
    }
  }
}