UNPKG

@milandadhaniya/tiny-logger-js

Version:

A tiny logger for both Node.js and browser environments, with configurable log levels and console output methods.

89 lines (87 loc) 2.64 kB
"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 index_exports = {}; __export(index_exports, { TinyLogger: () => TinyLogger, default: () => index_default }); module.exports = __toCommonJS(index_exports); var TinyLogger = class { constructor(config) { this.allowed = config?.allowed ?? /* @__PURE__ */ new Set([ "log", "info", "warn", "error", "debug", "table", "trace", "group", "groupEnd", "time", "timeEnd" ]); } shouldLog(type, force = false) { return force || this.allowed.has(type); } format(title, msg) { return title ? [title, msg] : [msg]; } log({ title, msg, force }) { if (this.shouldLog("log", force)) console.log(...this.format(title, msg)); } info({ title, msg, force }) { if (this.shouldLog("info", force)) console.info(...this.format(title, msg)); } warn({ title, msg, force }) { if (this.shouldLog("warn", force)) console.warn(...this.format(title, msg)); } error({ title, msg, force }) { if (this.shouldLog("error", force)) console.error(...this.format(title, msg)); } debug({ title, msg, force }) { if (this.shouldLog("debug", force)) console.debug(...this.format(title, msg)); } trace(msg) { if (this.shouldLog("trace")) console.trace(msg); } table({ msg, force }) { if (this.shouldLog("table", force)) console.table(msg); } group(label) { if (this.shouldLog("group")) console.group(label); } groupEnd() { if (this.shouldLog("groupEnd")) console.groupEnd(); } time(label) { if (this.shouldLog("time")) console.time(label); } timeEnd(label) { if (this.shouldLog("timeEnd")) console.timeEnd(label); } }; var logger = new TinyLogger(); var index_default = logger; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { TinyLogger });