@alwatr/logger
Version:
Fancy colorful console debugger with custom scope written in tiny TypeScript, ES module.
114 lines (111 loc) • 4.94 kB
JavaScript
/* @alwatr/logger v5.5.3 */
;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/main.ts
var main_exports = {};
__export(main_exports, {
createLogger: () => createLogger
});
module.exports = __toCommonJS(main_exports);
// src/logger.ts
var import_package_tracer = require("@alwatr/package-tracer");
var import_platform_info = require("@alwatr/platform-info");
__dev_mode__: import_package_tracer.packageTracer.add("@alwatr/logger", "5.5.3");
var defaultDebugMode = /* @__PURE__ */ (() => {
return import_platform_info.platformInfo.development || (import_platform_info.platformInfo.isCli ? Boolean(process.env.DEBUG) : typeof localStorage !== "undefined" && localStorage.getItem("ALWATR_DEBUG") === "1");
})();
var colorList = /* @__PURE__ */ (() => import_platform_info.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: import_platform_info.platformInfo.isCli ? "\x1B[{{color}}m" : "color: {{color}};",
reset: import_platform_info.platformInfo.isCli ? "\x1B[0m" : "color: inherit;"
}))();
var _keySection = /* @__PURE__ */ (() => import_platform_info.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: import_platform_info.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: import_platform_info.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: import_platform_info.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: import_platform_info.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")
};
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
createLogger
});
//# sourceMappingURL=main.cjs.map