UNPKG

missionlog

Version:

🚀 lightweight TypeScript abstract logger • level based filtering and optional tagging • supports both ESM & CJS

10 lines (9 loc) • 3.08 kB
"use strict";var d=Object.defineProperty;var p=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var w=Object.prototype.hasOwnProperty;var k=(r,e)=>{for(var n in e)d(r,n,{get:e[n],enumerable:!0})},F=(r,e,n,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of b(e))!w.call(r,l)&&l!==n&&d(r,l,{get:()=>e[l],enumerable:!(t=p(e,l))||t.enumerable});return r};var A=r=>F(d({},"__esModule",{value:!0}),r);var N={};k(N,{DEFAULT_TAG:()=>s,Log:()=>g,LogLevel:()=>R,log:()=>C,tag:()=>O});module.exports=A(N);/** * @module missionlog * @author Ray Martone * @copyright Copyright (c) 2019-2025 Ray Martone * @license MIT * @description A lightweight TypeScript logger with level-based filtering and tagging. * Drop-in replacement for console.log with additional categorization and filtering capabilities. */var o={TRACE:1,DEBUG:2,INFO:3,WARN:4,ERROR:5,OFF:6},R=(i=>(i.TRACE="TRACE",i.DEBUG="DEBUG",i.INFO="INFO",i.WARN="WARN",i.ERROR="ERROR",i.OFF="OFF",i))(R||{}),s="*",c=new Set,O=new Proxy({},{get(r,e){if(typeof e=="string"&&c.has(e))return e},ownKeys(){return Array.from(c)},getOwnPropertyDescriptor(){return{enumerable:!0,configurable:!0}}}),h=["OFF","TRACE","DEBUG","INFO","WARN","ERROR","OFF"],L=new Map([["TRACE",o.TRACE],["DEBUG",o.DEBUG],["INFO",o.INFO],["WARN",o.WARN],["ERROR",o.ERROR],["OFF",o.OFF]]),f=o.INFO,T=o.DEBUG,y=[],g=class{_defaultLevel=f;_tagToLevel=new Map;_levelCache=new Map;_callback;init(e,n){if(this._levelCache.clear(),e)for(let t of Object.keys(e))this._setTagLevel(t,e[t]);return n!==void 0&&(this._callback=n),this}_setTagLevel(e,n){let t=L.get(n);t!==void 0?e===s?this._defaultLevel=t:(this._tagToLevel.set(e,t),c.add(e)):(console.warn(`Invalid log level "${n}" for tag "${e}". Using default (${h[this._defaultLevel]}).`),this._tagToLevel.set(e,T),c.add(e))}_shouldLog(e,n){let t=n||s,l=this._levelCache.get(e);if(l){let a=l.get(t);if(a!==void 0)return a}let u=this._tagToLevel.get(t),i=e>=(u??this._defaultLevel);return(u!==void 0||t===s)&&(l||(l=new Map,this._levelCache.set(e,l)),l.set(t,i)),i}_log(e,n,...t){if(!this._callback||n===void 0)return;let l,u,i;if(typeof n=="string"&&c.has(n)?(l=n,u=t[0],i=t.slice(1)):(l=s,u=n,i=t),u===void 0||u===""||!this._shouldLog(e,l))return;let a;i.length===0?a=y:i.includes(void 0)?a=i.filter(_=>_!==void 0):a=i;let E=h[e],v=l===s?"":l;this._callback(E,v,u,a)}debug(e,...n){return this._log(o.DEBUG,e,...n),this}error(e,...n){return this._log(o.ERROR,e,...n),this}info(e,...n){return this._log(o.INFO,e,...n),this}log(e,...n){return this._log(o.INFO,e,...n),this}trace(e,...n){return this._log(o.TRACE,e,...n),this}warn(e,...n){return this._log(o.WARN,e,...n),this}isLevelEnabled(e,n=s){let t=L.get(e);return t===void 0?!1:this._shouldLog(t,n)}isDebugEnabled(e=s){return this.isLevelEnabled("DEBUG",e)}isTraceEnabled(e=s){return this.isLevelEnabled("TRACE",e)}reset(){return this._tagToLevel.clear(),this._levelCache.clear(),this._defaultLevel=f,this._callback=void 0,c.clear(),this}},C=new g;0&&(module.exports={DEFAULT_TAG,Log,LogLevel,log,tag}); //# sourceMappingURL=index.cjs.js.map