Files
Leantime/dev/DWGViewer/libs/shims/loglevel.js

33 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 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