missionlog
Version:
🚀 lightweight TypeScript abstract logger • level based filtering and optional tagging • supports both ESM & CJS
10 lines (9 loc) • 2.43 kB
JavaScript
/**
* @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 v=(l=>(l.TRACE="TRACE",l.DEBUG="DEBUG",l.INFO="INFO",l.WARN="WARN",l.ERROR="ERROR",l.OFF="OFF",l))(v||{}),A="*",o=new Set,f=new Proxy({},{get(g,e){if(typeof e=="string"&&o.has(e))return e},ownKeys(){return Array.from(o)},getOwnPropertyDescriptor(){return{enumerable:!0,configurable:!0}}}),a=new Map([[1,"TRACE"],[2,"DEBUG"],[3,"INFO"],[4,"WARN"],[5,"ERROR"],[6,"OFF"]]),L=new Map([["TRACE",1],["DEBUG",2],["INFO",3],["WARN",4],["ERROR",5],["OFF",6]]),c=3,R=2,_=[],u=class{_defaultLevel=c;_tagToLevel=new Map;_levelCache=new Map;_callback;init(e,n){if(this._levelCache.clear(),e)for(let t in e){let i=e[t];this._setTagLevel(t,i)}return n!==void 0&&(this._callback=n),this}_setTagLevel(e,n){let t=L.get(n);t!==void 0?e==="*"?this._defaultLevel=t:(this._tagToLevel.set(e,t),o.add(e)):(console.warn(`Invalid log level "${n}" for tag "${e}". Using default (${a.get(this._defaultLevel)}).`),this._tagToLevel.set(e,R),o.add(e))}_shouldLog(e,n){let t=this._levelCache.get(e);t||(t=new Map,this._levelCache.set(e,t));let i=n||"*",r=t.get(i);if(r!==void 0)return r;let l=this._tagToLevel.get(i)??this._defaultLevel,s=e>=l;return t.set(i,s),s}_log(e,n,...t){if(!this._callback||n===void 0)return;let i,r,l;if(typeof n=="string"&&o.has(n)?(i=n,r=t[0],l=t.slice(1)):(i="*",r=n,l=t),r===void 0||r===""||!this._shouldLog(e,i))return;let s=l.length>0?l.filter(E=>E!==void 0):_,h=a.get(e),d=i==="*"?"":i;this._callback(h,d,r,s)}debug(e,...n){return this._log(2,e,...n),this}error(e,...n){return this._log(5,e,...n),this}info(e,...n){return this._log(3,e,...n),this}log(e,...n){return this._log(3,e,...n),this}trace(e,...n){return this._log(1,e,...n),this}warn(e,...n){return this._log(4,e,...n),this}isLevelEnabled(e,n="*"){let t=L.get(e);return t===void 0?!1:this._shouldLog(t,n)}isDebugEnabled(e="*"){return this.isLevelEnabled("DEBUG",e)}isTraceEnabled(e="*"){return this.isLevelEnabled("TRACE",e)}reset(){return this._tagToLevel.clear(),this._levelCache.clear(),this._defaultLevel=c,this._callback=void 0,o.clear(),this}},p=new u;export{A as DEFAULT_TAG,u as Log,v as LogLevel,p as log,f as tag};
//# sourceMappingURL=index.esm.js.map