UNPKG

@holz/console-backend

Version:

A console backend for Holz

65 lines (64 loc) 1.85 kB
const r = { /** A critical failure happened and the program must exit. */ fatal: 60, /** Something failed, but we can keep going. */ error: 50, /** Cause for concern, but we can keep going. */ warn: 40, /** High-level progress updates. */ info: 30, /** Verbose update about events or control flow (usually hidden). */ debug: 20, /** Extremely detailed progress updates (usually hidden). */ trace: 10 }, c = 1e3, i = 60 * c, u = 60 * i, d = 24 * u, h = (a, s = a) => { const e = a.getTime() - s.getTime(), t = Math.abs(e), n = e < 0 ? "" : "+"; return t >= d ? `${n}${Math.round(e / d)}d` : t >= u ? `${n}${Math.round(e / u)}h` : t >= i ? `${n}${Math.round(e / i)}m` : t >= c ? `${n}${Math.round(e / c)}s` : `${n}${e}ms`; }, p = (a = {}) => { const s = a.console ?? console; let e; return (t) => { const n = new Date(t.timestamp), { error: l, ...f } = t.context, m = [ { include: !0, format: "%s", values: [t.message] }, { include: Object.keys(f).length > 0, format: "%o", // Chrome hides object content with `%O`. values: [f] }, { include: !0, format: "%c%s", values: ["color: gray", h(n, e)] }, { include: t.origin.length > 0, format: "%c%s", values: [ "color: rgba(128, 128, 128, 0.6); font-style: italic", t.origin.join(":") ] }, { include: l, format: "%o", values: [l] } ].filter((o) => o.include), $ = m.map((o) => o.format).join(" "), g = m.flatMap((o) => o.values); s[v[t.level]]($, ...g), e = n; }; }, v = { [r.trace]: "trace", [r.debug]: "debug", [r.info]: "info", [r.warn]: "warn", [r.error]: "error", [r.fatal]: "error" }; export { p as createConsoleBackend };