All files / packages/logger/src/format index.ts

0% Statements 0/60
0% Branches 0/1
0% Functions 0/1
0% Lines 0/60

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                                                                                                                                                         
import { camelCase, importGlob } from '@https-enable/utils'
import format from './format'
import levels from './levels'
 
import align from './plugins/align'
import cli from './plugins/cli'
import colorize from './plugins/colorize'
import combine from './plugins/combine'
import errors from './plugins/errors'
import json from './plugins/json'
import label from './plugins/label'
import logstash from './plugins/logstash'
import metadata from './plugins/metadata'
import ms from './plugins/ms'
import padLevels from './plugins/pad-levels'
import prettyPrint from './plugins/pretty-print'
import printf from './plugins/printf'
import simple from './plugins/simple'
import splat from './plugins/splat'
import timestamp from './plugins/timestamp'
import uncolorize from './plugins/uncolorize'
 
/*
 * @api private
 * method {function} exposeFormat
 * 以惰性加载的 getter 方式在主格式对象上暴露子格式。
 */
function exposeFormat(name: string, requireFormat: () => any) {
  Object.defineProperty(format, name, {
    get() {
      return requireFormat()
    },
    configurable: true,
  })
}
 
exposeFormat('align', () => align)
exposeFormat('cli', () => cli)
exposeFormat('colorize', () => colorize)
exposeFormat('combine', () => combine)
exposeFormat('errors', () => errors)
exposeFormat('json', () => json)
exposeFormat('label', () => label)
exposeFormat('logstash', () => logstash)
exposeFormat('metadata', () => metadata)
exposeFormat('ms', () => ms)
exposeFormat('padLevels', () => padLevels)
exposeFormat('prettyPrint', () => prettyPrint)
exposeFormat('printf', () => printf)
exposeFormat('simple', () => simple)
exposeFormat('splat', () => splat)
exposeFormat('timestamp', () => timestamp)
exposeFormat('uncolorize', () => uncolorize)
 
/**
 * @deprecated 动态批量引用是异步的,开发使用体验不好,暂时弃用
 */
export async function initFormats() {
  const routeModules = await importGlob('./plugins/*.ts', {
    eager: true,
    import: 'default',
  })
  Object.entries(routeModules).forEach(([key, value]) => {
    const ext = key.split('.').pop()
    const name = key.split('/').pop()?.replace(ext ? `.${ext}` : '', '')
    if (name) {
      exposeFormat(camelCase(name), () => value)
    }
  })
  return format
}
 
export default format
export { format, levels }
 
export type * from './type'