All files / packages/logger/src/transports base.ts

0% Statements 0/28
100% Branches 1/1
100% Functions 1/1
0% Lines 0/28

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                                                                                       
import type { TransformableInfo } from '../format'
import type { LogEntry } from '../type'
import type { TransportBaseOptions, TransportErrorCallback } from './type'
import os from 'node:os'
import { ColorStringRE } from '@https-enable/utils'
import { levels } from '../format'
import { configs } from '../triple-beam'
 
export abstract class Transport<T extends TransportBaseOptions> {
  public level?: LogEntry['level']
  protected eol?: string
  protected name?: string
  protected levels: Record<string, number>
 
  protected options: T
 
  constructor(options: T) {
    this.options = options
 
    this.name = options.name
    this.level = options.level
    this.levels = options.levels || levels(configs.npm).levels
    this.eol = typeof options.eol === 'string' ? options.eol : os.EOL
  }
 
  // 判断是否满足日志级别要求
  protected shouldLog(level: string, force = false) {
    level = level.replace(ColorStringRE, '')
 
    const levels = Object.keys(this.levels).reverse()
    return force || levels.indexOf(level) >= levels.indexOf(this.level || 'INFO')
  }
 
  // 使用局部format
  protected selfFormat(options?: { level: LogEntry['level'], message: string }) {
    if (this.options.format && options) {
      return this.options.format.transform(options)
    }
  }
 
  // 日志输出抽象定义
  public abstract log(info: TransformableInfo, callback?: TransportErrorCallback, options?: { level: LogEntry['level'], message: string }, force?: boolean): void
}