UNPKG

missionlog

Version:

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

19 lines (18 loc) • 3.04 kB
/** * @module missionlog * @author Ray Martone * @copyright Copyright (c) 2019-2025 Ray Martone * @license MIT * @description A lightweight TypeScript logger providing level-based filtering and tagging capabilities. * missionlog is designed as a drop-in replacement for console.log with additional features for * categorizing and filtering logs by severity levels and custom tags. * * Key features: * - Type safety with LogMessage and LogConfig interfaces * - Performance optimizations with level caching * - Enhanced API with structured logging support via EnhancedLogCallback * - Fully chainable API with all methods returning the logger instance * - Level checking with isLevelEnabled() and configuration reset() * - Full backward compatibility with existing logging patterns */var d=(i=>(i.TRACE="TRACE",i.DEBUG="DEBUG",i.INFO="INFO",i.WARN="WARN",i.ERROR="ERROR",i.OFF="OFF",i))(d||{}),s="*",a=new Set,v=new Proxy({},{get(g,e){if(typeof e=="string"&&a.has(e))return e},ownKeys(){return Array.from(a)},getOwnPropertyDescriptor(){return{enumerable:!0,configurable:!0}}}),u=new Map([[1,"TRACE"],[2,"DEBUG"],[3,"INFO"],[4,"WARN"],[5,"ERROR"],[6,"OFF"]]),h=new Map([["TRACE",1],["DEBUG",2],["INFO",3],["WARN",4],["ERROR",5],["OFF",6]]),o=class{_defaultLevel=3;_tagToLevel=new Map;_levelCache=new Map;_callback;_enhancedCallback;init(e,t){if(this._levelCache.clear(),e)for(let n in e){let r=e[n];this._setTagLevel(n,r)}return t!==void 0&&(this._callback=t),this}_setTagLevel(e,t){let n=h.get(t);n!==void 0?e===s?this._defaultLevel=n:(this._tagToLevel.set(e,n),a.add(e)):(console.warn(`Invalid log level "${t}" for tag "${e}". Using default (${u.get(this._defaultLevel)}).`),this._tagToLevel.set(e,2),a.add(e))}setEnhancedCallback(e){return this._enhancedCallback=e,this}_shouldLog(e,t){let n=`${e}:${t||s}`;if(this._levelCache.has(n))return this._levelCache.get(n);let r=this._tagToLevel.get(t||s)??this._defaultLevel,l=e>=r;return this._levelCache.set(n,l),l}_log(e,t,...n){if(!this._callback&&!this._enhancedCallback||t===void 0)return;let r="",l;if(typeof t=="string"&&a.has(t)?(r=t,l=n[0]??"",n=n.slice(1)):l=t,l===void 0||l===""||!this._shouldLog(e,r))return;let i=n.filter(L=>L!==void 0),c=u.get(e);this._callback&&this._callback(c,r,l,i),this._enhancedCallback&&this._enhancedCallback({level:c,tag:r,message:l,params:i,timestamp:new Date})}debug(e,...t){return this._log(2,e,...t),this}error(e,...t){return this._log(5,e,...t),this}info(e,...t){return this._log(3,e,...t),this}log(e,...t){return this._log(3,e,...t),this}trace(e,...t){return this._log(1,e,...t),this}warn(e,...t){return this._log(4,e,...t),this}isLevelEnabled(e,t=s){let n=h.get(e);return n===void 0?!1:this._shouldLog(n,t)}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=3,this}},R=new o;export{s as DEFAULT_TAG,o as Log,d as LogLevel,R as log,v as tag}; //# sourceMappingURL=index.esm.js.map