33 lines
1.1 KiB
JavaScript
33 lines
1.1 KiB
JavaScript
/**
|
||
* loglevel 的极简替身。
|
||
*
|
||
* dxf-parser 依赖 loglevel 打调试日志,但它只用到 setLevel/debug/info/warn/error/trace
|
||
* 这几个方法。为了让 vendored 的 dxf-parser 原样能在浏览器里跑(不引入打包工具),
|
||
* index.html 的 importmap 把 "loglevel" 指到这里。
|
||
* 默认只放行 warn 及以上,避免解析大图时刷屏。
|
||
*/
|
||
|
||
const LEVELS = { trace: 0, debug: 1, info: 2, warn: 3, error: 4, silent: 5 }
|
||
let level = LEVELS.warn
|
||
|
||
const emit = (name, consoleFn) => (...args) => {
|
||
if (LEVELS[name] < level) return
|
||
// dxf-parser 里有 '%s' 风格的格式串,console 原生就支持
|
||
consoleFn.apply(console, args)
|
||
}
|
||
|
||
const log = {
|
||
levels: LEVELS,
|
||
setLevel(l) { level = typeof l === 'number' ? l : (LEVELS[String(l).toLowerCase()] ?? LEVELS.warn) },
|
||
getLevel() { return level },
|
||
getLogger() { return log },
|
||
trace: emit('trace', console.debug),
|
||
debug: emit('debug', console.debug),
|
||
info: emit('info', console.info),
|
||
warn: emit('warn', console.warn),
|
||
error: emit('error', console.error),
|
||
}
|
||
log.log = log.debug
|
||
|
||
export default log
|