UNPKG

@alwatr/logger

Version:

Fancy colorful console debugger with custom scope written in tiny TypeScript, ES module.

88 lines (86 loc) 3.66 kB
/* @alwatr/logger v5.5.3 */ // src/logger.ts import { packageTracer } from "@alwatr/package-tracer"; import { platformInfo } from "@alwatr/platform-info"; __dev_mode__: packageTracer.add("@alwatr/logger", "5.5.3"); var defaultDebugMode = /* @__PURE__ */ (() => { return platformInfo.development || (platformInfo.isCli ? Boolean(process.env.DEBUG) : typeof localStorage !== "undefined" && localStorage.getItem("ALWATR_DEBUG") === "1"); })(); var colorList = /* @__PURE__ */ (() => platformInfo.isCli ? ["0;36", "0;35", "0;34", "0;33", "0;32"] : [ "#35b997", "#f05561", "#ee224a", "#91c13e", "#22af4b", "#f0e995", "#0fe995", "#0f89ca", "#08b9a5", "#fee851", "#ee573d", "#f9df30", "#1da2dc", "#f05123", "#ee2524" ])(); var _colorIndex = 0; var _getNextColor = () => { const color = colorList[_colorIndex]; _colorIndex++; if (_colorIndex >= colorList.length) { _colorIndex = 0; } return color; }; var _style = /* @__PURE__ */ (() => ({ scope: platformInfo.isCli ? "\x1B[{{color}}m" : "color: {{color}};", reset: platformInfo.isCli ? "\x1B[0m" : "color: inherit;" }))(); var _keySection = /* @__PURE__ */ (() => platformInfo.isCli ? "%s%s%s" : "%c%s%c")(); var _sanitizeDomain = (domain) => { domain = domain.trim(); const first = domain.charAt(0); if (first !== "[" && first !== "{" && first !== "<") { domain = "[" + domain + "]"; } return domain; }; var createLogger = (domain, debugMode = defaultDebugMode) => { const color = _getNextColor(); const styleScope = _style.scope.replace("{{color}}", color); domain = _sanitizeDomain(domain); const requiredItems = { debugMode, banner: platformInfo.isCli ? console.log.bind(console, `\x1B[1;37;45m {{{ %s }}} ${_style.reset}`) : console.log.bind( console, "%c%s", "font-size: 2rem; background-color: #5858e8; color: #fff; padding: 1rem 4rem; border-radius: 0.5rem;" ), accident: platformInfo.isCli ? console.warn.bind(console, `${styleScope}⚠️ %s\x1B[33m.%s() Accident \`%s\`!${_style.reset}`, domain) : console.warn.bind(console, "%c%s%c.%s() Accident `%s`!", styleScope, domain, _style.reset), error: platformInfo.isCli ? console.error.bind(console, `${styleScope}❌ %s\x1B[31m.%s() Error \`%s\`${_style.reset} `, domain) : console.error.bind(console, "%c%s%c.%s() Error `%s`\n", styleScope, domain, _style.reset) }; if (!debugMode) { return requiredItems; } return { ...requiredItems, logProperty: console.debug.bind(console, _keySection + ".%s = %o;", styleScope, domain, _style.reset), logMethod: console.debug.bind(console, _keySection + ".%s();", styleScope, domain, _style.reset), logFileModule: console.debug.bind(console, _keySection + "/%s.js;", styleScope, domain, _style.reset), logMethodArgs: console.debug.bind(console, _keySection + ".%s(%o);", styleScope, domain, _style.reset), logMethodFull: console.debug.bind(console, _keySection + ".%s(%o) => %o", styleScope, domain, _style.reset), logStep: console.debug.bind(console, _keySection + ".%s() -> %s", styleScope, domain, _style.reset), logOther: console.debug.bind(console, _keySection, styleScope, domain, _style.reset), incident: platformInfo.isCli ? console.log.bind(console, `${styleScope}🚸 %s${_style.reset}.%s() Incident \`%s\`!${_style.reset}`, domain) : console.log.bind(console, "%c%s%c.%s() Incident `%s`!", styleScope, domain, "color: orange;"), time: (label) => console.time(domain + "." + label + " duration time"), timeEnd: (label) => console.timeEnd(domain + "." + label + " duration time") }; }; export { createLogger }; //# sourceMappingURL=main.mjs.map