"use strict"; 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/index.ts var src_exports = {}; __export(src_exports, { CODES: () => CODES, LOG_LEVELS: () => LOG_LEVELS, Logger: () => Logger, MonoEffect: () => MonoEffect, PolyEffect: () => PolyEffect, default: () => logger_default }); module.exports = __toCommonJS(src_exports); // src/types.ts var LOG_LEVELS = { debug: 0, verbose: 1, info: 2, warn: 3, error: 4, fatal: 5 }; // src/effect/effect.ts var MonoEffect = class { constructor(effect, level) { this.effect = effect; this.level = level; if (typeof effect !== "function") { throw new Error("MonoLogger/MonoEffect: effect must be a function"); } if (level) { this.apply = (ts, input_level, topics, ...messages) => { if (LOG_LEVELS[input_level] >= LOG_LEVELS[level]) { return effect(ts, input_level, topics, ...messages); } }; } else { this.apply = effect; } } apply; }; var PolyEffect = class _PolyEffect { constructor(level) { this.level = level; } _effects = new Array(0); effect = (ts, input_level, topics, ...messages) => { if (this.level && LOG_LEVELS[input_level] < LOG_LEVELS[this.level]) return; for (const effect of this._effects) { if (effect instanceof MonoEffect || effect instanceof _PolyEffect) { effect.apply(ts, input_level, topics, ...messages); } else if (typeof effect === "function") { effect(ts, input_level, topics, ...messages); } } }; add(effect) { if (typeof effect !== "function" && !(effect instanceof MonoEffect) && !(effect instanceof _PolyEffect)) { throw new Error( "MonoLogger/PolyEffect: Effect must be a function, MonoEffect or PolyEffect" ); } this._effects.push(effect); } apply = this.effect.bind(this); }; // src/colors.ts var COLORS = { debug: (s) => `\x1B[90m${s}\x1B[39m`, warn: (s) => `\x1B[33m${s}\x1B[39m`, info: (s) => `\x1B[32m${s}\x1B[39m`, error: (s) => `\x1B[31m${s}\x1B[39m`, verbose: (s) => `\x1B[36m${s}\x1B[39m`, fatal: (s) => `\x1B[35m${s}\x1B[39m` }; // src/logger.ts var DEFAULT_FORMATTER = new Intl.DateTimeFormat("en-US", { hour: "numeric", minute: "numeric", second: "numeric", hour12: false, month: "numeric", day: "numeric", year: "numeric" }); var CODES = { debug: "DBG", warn: "WRN", info: "INF", error: "ERR", verbose: "VRB", fatal: "FTL" }; var METHOD = { debug: "log", verbose: "log", info: "info", warn: "warn", error: "error", fatal: "error" }; var Logger = class _Logger { constructor(subject, _config, _parent) { this.subject = subject; this._config = _config; this._parent = _parent; const prefix = this._parent ? this._parent.topics : []; this.topics = this.subject ? [...prefix, this.subject] : prefix; this._topics_str = this.topics.map((t) => COLORS["fatal"](t)).join(":"); this._date_formatter = _config?.date_format ?? DEFAULT_FORMATTER.format; } /** * A list of topics for current logger instance. * The list will include all the parents topics ordered from **root** logger to **leaf** */ topics; _topics_str; _date_formatter; // Factories /** * Create a new descendant logger with specified topic * @param topic {string} New logger topic name * @param opts {iLoggerConfig} New logger configuration (if not provided, will use it's ancestors config) * @returns A new Logger with specified topic */ topic(topic, opts) { return new _Logger(topic, opts ?? this._config, this); } // Formatters display_params(ts, lvl) { const display_ts = COLORS["debug"](this._date_formatter(ts)); const level = `[${COLORS[lvl](CODES[lvl])}]`; let output = `${display_ts} ${level}`; if (this._config?.prefix) output += ` ${this._config?.prefix()}`; if (this._topics_str) output += ` ${this._topics_str}`; return output; } _transform(args) { return args.map((el) => { if (this._config?.transform) return this._config.transform(el); return el; }); } // Methods /** * Alias for ```DEBUG``` method. * Uses ```console.log``` * @param ...arg {any} Message or object to log */ log = (...args) => this._log("debug", args); /** * Logs a ```DEBUG``` message * Uses ```console.log``` * @param ...arg {any} Message or object to log */ debug = (...args) => this._log("debug", args); /** * Logs a ```VERBOSE``` message * Uses ```console.verbose``` * @param ...arg {any} Message or object to log */ verbose = (...args) => this._log("verbose", args); /** * Logs an ```INFO``` message * Uses ```console.info``` * @param ...arg {any} Message or object to log */ info = (...args) => this._log("info", args); /** * Logs a ```WARN``` message * Uses ```console.warn``` * @param ...arg {any} Message or object to log */ warn = (...args) => this._log("warn", args); /** * Logs an ```ERROR``` message * Uses ```console.error``` * @param ...arg {any} Message or object to log */ error = (...args) => this._log("error", args); /** * Logs a ```FATAL``` error message * Uses ```console.error``` * @param ...arg {any} Message or object to log */ fatal = (...args) => this._log("fatal", args); _log(level, args) { const ts = /* @__PURE__ */ new Date(); if (LOG_LEVELS[this._config?.level ?? "debug"] > LOG_LEVELS[level]) { if (this._config?.force_effect) this._run_effect(ts, level, this.topics, ...args); return; } const transformed = this._transform(args); console[METHOD[level]](this.display_params(ts, level), ...transformed); this._run_effect(ts, level, ...args); } _run_effect(ts, level, ...messages) { const effect = this._config?.effect; if (!effect) return; if (effect instanceof MonoEffect || effect instanceof PolyEffect) { effect.apply(ts, level, this.topics, ...messages); } else if (typeof effect === "function") { effect(ts, level, this.topics, ...messages); } } }; var logger_default = new Logger(); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { CODES, LOG_LEVELS, Logger, MonoEffect, PolyEffect }); //# sourceMappingURL=index.cjs.map