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