@ncoderz/log-m8 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -67,7 +67,6 @@ Log-m8 provides multiple logging levels in ascending order of verbosity:
67
67
  | `track` | Analytics and user behavior tracking events |
68
68
  | `trace` | Most detailed execution information for fine-grained debugging |
69
69
 
70
- When a logger is set to a specific level, it emits events at that level and all levels above it in the list. For example, a logger set to `info` will emit `fatal`, `error`, `warn`, and `info` events, but not `debug`, `track`, or `trace` events.
71
70
 
72
71
  ### Hierarchical Loggers
73
72
 
@@ -76,7 +75,7 @@ Loggers are organized hierarchically using dot notation:
76
75
  ```typescript
77
76
  const appLogger = LogM8.getLogger('app');
78
77
  const dbLogger = LogM8.getLogger('app.database');
79
- const cacheLogger = LogM8.getLogger('app.cache');
78
+ const cacheLogger = appLogger.getLogger('cache'); // 'app.cache'
80
79
  ```
81
80
 
82
81
  This allows you to configure logging granularly by component while maintaining a clean organizational structure.
@@ -102,7 +101,8 @@ LogM8.init({
102
101
  name: 'console',
103
102
  formatter: 'default-formatter',
104
103
  // Optional priority (higher runs first)
105
- priority: 100
104
+ priority: 100,
105
+ color: true
106
106
  },
107
107
  {
108
108
  name: 'file',
@@ -110,7 +110,6 @@ LogM8.init({
110
110
  formatter: {
111
111
  name: 'default-formatter',
112
112
  timestampFormat: 'yyyy-MM-dd hh:mm:ss.SSS',
113
- color: false
114
113
  }
115
114
  }
116
115
  ],
@@ -260,6 +259,46 @@ LogM8.enableFilter('sensitive-data', 'console');
260
259
 
261
260
  ```
262
261
 
262
+ ### Adjusting log levels at runtime
263
+
264
+ You can change the effective logging thresholds without recreating loggers:
265
+
266
+ ```typescript
267
+ // Set global log level gate (does not change individual logger levels)
268
+ LogM8.setLevel('info');
269
+
270
+ // Set a specific logger's level via manager (equivalent to getLogger(...).setLevel)
271
+ LogM8.setLevel('debug', 'app.database');
272
+
273
+ // Or use the logger instance directly
274
+ const alpha = LogM8.getLogger('alpha');
275
+ alpha.setLevel('warn');
276
+ ```
277
+
278
+ Behavior rules:
279
+
280
+ - A message is emitted only if its level is enabled by BOTH the logger’s level and the global level.
281
+ - Think of the effective level as the stricter bound: effective = min(loggerLevel, globalLevel) in the level order
282
+ (off < fatal < error < warn < info < debug < track < trace).
283
+
284
+ Example sequence:
285
+
286
+ ```typescript
287
+ const warnLogger = LogM8.getLogger('alpha');
288
+ warnLogger.setLevel('warn');
289
+ const debugLogger = LogM8.getLogger('beta');
290
+ debugLogger.setLevel('debug');
291
+
292
+ // Global: info → beta logs info+, alpha logs warn+
293
+ LogM8.setLevel('info');
294
+
295
+ // Global: debug → beta logs debug+, alpha still logs warn+
296
+ LogM8.setLevel('debug');
297
+
298
+ // Global: info again → beta logs info+, alpha still logs warn+
299
+ LogM8.setLevel('info');
300
+ ```
301
+
263
302
  ## Extending with Custom Plugins
264
303
 
265
304
  You can extend log-m8 with custom appenders, formatters, and filters.
@@ -296,9 +335,9 @@ LogM8.init({
296
335
  Log-m8 works in browsers with automatic environment detection:
297
336
 
298
337
  ```html
299
- <script src="https://cdn.jsdelivr.net/npm/log-m8/dist/browser/log-m8.global.js"></script>
338
+ <script src="https://cdn.jsdelivr.net/npm/@ncoderz/log-m8@latest/dist/browser/log-m8.global.js"></script>
300
339
  <script>
301
- const { LogM8 } = window.LogM8;
340
+ const { LogM8 } = window.logM8;
302
341
 
303
342
  LogM8.init({
304
343
  level: 'info',
@@ -1 +1 @@
1
- "use strict";var logM8=(()=>{var t,e=Object.defineProperty,i=Object.getOwnPropertyDescriptor,s=Object.getOwnPropertyNames,r=Object.prototype.hasOwnProperty,n=(t=function(t){if("undefined"!=typeof require)return require.apply(this,arguments);throw Error('Dynamic require of "'+t+'" is not supported')},"undefined"!=typeof require?require:"undefined"!=typeof Proxy?new Proxy(t,{get:(t,e)=>("undefined"!=typeof require?require:t)[e]}):t),o=(t,i,s)=>((t,i,s)=>i in t?e(t,i,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[i]=s)(t,"symbol"!=typeof i?i+"":i,s),l={};((t,i)=>{for(var s in i)e(t,s,{get:i[s],enumerable:!0})})(l,{LogLevel:()=>h,LogM8Utils:()=>_,Logging:()=>K,PluginKind:()=>a});var h={off:"off",fatal:"fatal",error:"error",warn:"warn",info:"info",debug:"debug",track:"track",trace:"trace"},a={appender:"appender",filter:"filter",formatter:"formatter"},c="console",u="1.0.0",f=a.appender,d=new Set([h.fatal,h.error,h.warn,h.info,h.debug,h.track,h.trace]),g=class{constructor(){o(this,"name",c),o(this,"version",u),o(this,"kind",f),o(this,"supportedLevels",d),o(this,"enabled",!0),o(this,"priority"),o(this,"_config"),o(this,"_formatter"),o(this,"_filters",[]),o(this,"_available",!0),o(this,"off",()=>{}),o(this,"fatal",console.error?console.error.bind(console):console.log.bind(console)),o(this,"error",console.error?console.error.bind(console):console.log.bind(console)),o(this,"warn",console.warn?console.warn.bind(console):console.log.bind(console)),o(this,"info",console.info?console.info.bind(console):console.log.bind(console)),o(this,"debug",console.debug?console.debug.bind(console):console.log.bind(console)),o(this,"trace",console.debug?console.debug.bind(console):console.log.bind(console)),o(this,"track",console.log.bind(console))}init(t,e,i){var s,r;this.t=t,this.i=e,this.o=i||[],this.l="undefined"!=typeof console&&!!console.log,this.enabled=!1!==(null==(s=this.t)?void 0:s.enabled),this.priority=null==(r=this.t)?void 0:r.priority}dispose(){}write(t){if(!this.l)return;for(let e of this.o)if(e.enabled&&!e.filter(t))return;let e=this.i?this.i.format(t):[t];this[t.level](...e)}flush(){}enableFilter(t){let e=this.h(t);e&&(e.enabled=!0)}disableFilter(t){let e=this.h(t);e&&(e.enabled=!1)}h(t){return this.o.find(e=>e.name===t)}},m=class{constructor(){o(this,"name",c),o(this,"version",u),o(this,"kind",f)}create(t){let e=new g;return e.init(t),e}},p=n("fs"),y=/(yyyy|SSS|hh|mm|ss|SS|zz|z|yy|MM|dd|A|a|h|S)/g,b=new Set(["name","message","stack","cause"]),v=["code","errno","syscall","path"],_=class t{static isBrowser(){return"undefined"!=typeof window&&void 0!==window.document}static isString(t){return"string"==typeof t||t instanceof String}static getPropertyByPath(t,e){let i=t,s=e.replace(/\[(\d+)\]/g,".$1").split(".");for(let t of s){if("object"!=typeof i||null===i)return;if(Array.isArray(i)){let e=Number(t);if(Number.isInteger(e)&&e>=0){i=i[e];continue}}i=i[t]}return i}static formatTimestamp(t,e){let i=null==e?void 0:e.toLowerCase();if(!e||"iso"===i||"toisostring"===i)return t.toISOString();if("locale"===i||"tolocalestring"===i)return t.toLocaleString();let s=(t,e=2)=>String(t).padStart(e,"0"),r=t.getHours(),n=r%12==0?12:r%12;return e.replace(y,e=>{switch(e){case"yyyy":return s(t.getFullYear(),4);case"yy":return s(t.getFullYear()%100);case"MM":return s(t.getMonth()+1);case"dd":return s(t.getDate());case"hh":return s(r);case"h":return s(n);case"mm":return s(t.getMinutes());case"ss":return s(t.getSeconds());case"SSS":return s(t.getMilliseconds(),3);case"SS":return s(Math.floor(t.getMilliseconds()/10),2);case"S":return s(Math.floor(t.getMilliseconds()/100),1);case"A":return r<12?"AM":"PM";case"a":return r<12?"am":"pm";case"z":case"zz":{let i=-t.getTimezoneOffset(),r=i>=0?"+":"-",n=Math.floor(Math.abs(i)/60),o=Math.abs(i)%60;return"z"===e?`${r}${s(n)}:${s(o)}`:`${r}${s(n)}${s(o)}`}default:return e}})}static stringifyLog(e,{maxDepth:i=3,maxStringLength:s=200,maxArrayLength:r=100}={},n){let o=new WeakMap;return JSON.stringify(e,function(e,n){var l;if(n&&"object"==typeof n){let t=null!=(l=o.get(this))?l:0;if(t>=i)return Array.isArray(n)?"[Array]":"[Object]";if(o.set(n,t+1),Array.isArray(n)&&n.length>r){let t=n.slice(0,r);return t.push(`... ${n.length-r} more items`),t}}return t.isString(n)&&n.length>s?n.slice(0,s)+"…":"bigint"==typeof n?n.toString():n instanceof Date?n.toISOString():n instanceof Error?t.serializeError(n):n},n)}static serializeError(t){let e=(t,i)=>{if(!t)return null;let s=t;if("object"==typeof s&&null!==s){if(i.has(s))return{name:"CircularReference",message:"Circular reference detected in error cause chain"};i.add(s)}if("function"==typeof s.toJSON)try{let t=s.toJSON();return JSON.stringify(t),t}catch(t){}let r={name:s.name||"Error",message:s.message||"",stack:s.stack};"cause"in s&&void 0!==s.cause&&(r.cause=e(s.cause,i));for(let t in s)if(Object.prototype.hasOwnProperty.call(s,t)&&!b.has(t))try{let e=s[t];JSON.stringify(e),r[t]=e}catch(t){}for(let t of v)try{let e=Object.getOwnPropertyDescriptor(s,t);e&&void 0!==e.value&&(JSON.stringify(e.value),r[t]=e.value)}catch(t){}return r};return e(t,new WeakSet)}},w="file",S="1.0.0",L=a.appender,j=new Set([h.fatal,h.error,h.warn,h.info,h.debug,h.track,h.trace]),M=class{constructor(){o(this,"name",w),o(this,"version",S),o(this,"kind",L),o(this,"supportedLevels",j),o(this,"enabled",!0),o(this,"priority"),o(this,"_config"),o(this,"_formatter"),o(this,"_filters",[]),o(this,"_stream")}init(t,e,i){var s,r;this.t=t,this.i=e,this.o=i||[];let n=this.t.append?"a":"w";this.u=(0,p.createWriteStream)(this.t.filename,{flags:n}),this.enabled=!1!==(null==(s=this.t)?void 0:s.enabled),this.priority=null==(r=this.t)?void 0:r.priority}dispose(){var t;null==(t=this.u)||t.end()}write(t){if(!this.u)return;for(let e of this.o)if(e.enabled&&!e.filter(t))return;let e=(this.i?this.i.format(t):[t]).map(t=>_.isString(t)?t:String(t)).join(" ");this.u.write(e+"\n")}flush(){}enableFilter(t){let e=this.h(t);e&&(e.enabled=!0)}disableFilter(t){let e=this.h(t);e&&(e.enabled=!1)}h(t){return this.o.find(e=>e.name===t)}},O=class{constructor(){o(this,"name",w),o(this,"version",S),o(this,"kind",L)}create(t){let e=new M;return e.init(t),e}},k=class{constructor(){o(this,"name","match-filter"),o(this,"version","1.0.0"),o(this,"kind",a.filter),o(this,"enabled",!0),o(this,"_allow"),o(this,"_deny")}init(t){var e,i;let s=null!=t?t:{};this.m=null!=(e=s.allow)?e:void 0,this.p=null!=(i=s.deny)?i:void 0,this.enabled=!1!==s.enabled}dispose(){}filter(t){try{if(this.m&&Object.keys(this.m).length>0)for(let[e,i]of Object.entries(this.m)){let s=_.getPropertyByPath(t,e);if(!this.v(s,i))return!1}if(this.p&&Object.keys(this.p).length>0)for(let[e,i]of Object.entries(this.p)){let s=_.getPropertyByPath(t,e);if(this.v(s,i))return!1}return!0}catch(t){return!1}}v(t,e){if(t===e)return!0;if("number"==typeof t&&"number"==typeof e)return Number.isNaN(t)&&Number.isNaN(e);if(t instanceof Date&&e instanceof Date)return t.getTime()===e.getTime();if(Array.isArray(t)&&Array.isArray(e)){if(t.length!==e.length)return!1;for(let i=0;i<t.length;i++)if(!this.v(t[i],e[i]))return!1;return!0}if(this._(t)&&this._(e)){let i=Object.keys(t),s=Object.keys(e);if(i.length!==s.length)return!1;for(let s of i)if(!Object.prototype.hasOwnProperty.call(e,s)||!this.v(t[s],e[s]))return!1;return!0}return!1}_(t){return"object"==typeof t&&null!==t&&!Array.isArray(t)&&Object.getPrototypeOf(t)===Object.prototype}},A=class{constructor(){o(this,"name","match-filter"),o(this,"version","1.0.0"),o(this,"kind",a.filter)}create(t){let e=new k;return e.init(t),e}},F="default-formatter",$="1.0.0",E=a.formatter,x=["{timestamp} {LEVEL} [{logger}]","{message}","{data}"],P="hh:mm:ss.SSS",z=class{constructor(){o(this,"name",F),o(this,"version",$),o(this,"kind",E),o(this,"_config"),o(this,"_format"),o(this,"_timestampFormat",P),o(this,"_levelMap"),o(this,"_colorEnabled",!1),o(this,"_levelColorMap",{trace:"",track:"",debug:"",info:"",warn:"",error:"",fatal:""}),o(this,"_levelCssColorMap",{trace:"color: #bbb;",track:"color: orange;",debug:"color: grey;",info:"color: blue;",warn:"color: gold;",error:"color: red;",fatal:"background: red; color: white;"})}init(t){var e,i;let s=_.isBrowser();this.t=Object.assign({},t),this.S=!!this.t.color,this.L=[];let r=null!=(e=this.t.format)?e:x;if("string"==typeof this.t.format&&(r=[this.t.format]),r)for(let t of r){let e,i=/(\{[^}]+\})|([^{}]+)/g,s=[];for(;null!==(e=i.exec(t));)e[1]?s.push(e[1]):void 0!==e[2]&&s.push(e[2]);this.L.push(s)}this.j=null!=(i=this.t.timestampFormat)?i:P;let n=Object.values(h),o=Math.max(...n.map(t=>t.length));this.M=n.reduce((t,e)=>{let i=e.toUpperCase().padEnd(o," ");if(this.S){if(s){let s=this.O[e]||"";return t[e]=[`%c${i}`,s],t}i=(this.k[e]||"")+i+""}return t[e]=i,t},{})}dispose(){}format(t){let e,i=this.L;if(i.length>0){e=i.map(e=>1===e.length?this.resolveToken(e[0],t):e.reduce((e,i)=>{let s=this.resolveToken(i,t);return e+String(s)},""));let s=e.findIndex(t=>Array.isArray(t));if(s>=0){let t=e[s];t.length>0?e.splice(s,1,...t):e.splice(s,1)}}else e=[_.formatTimestamp(t.timestamp,this.j),t.level,t.logger,t.message,...t.data,t.context];return e}resolveToken(t,e){var i;if(t.startsWith("{")&&t.endsWith("}")){let s=t.slice(1,-1);if("message"===s&&"string"!=typeof e.message)return e.message;if("LEVEL"===s)return null!=(i=this.M[e.level])?i:e.level;if("timestamp"===s){let t=_.getPropertyByPath(e,s);return _.formatTimestamp(t,this.j)}return _.getPropertyByPath(e,s)}return t}},q=class{constructor(){o(this,"name",F),o(this,"version",$),o(this,"kind",E)}create(t){let e=new z;return e.init(t),e}},D="json-formatter",N="1.0.0",C=a.formatter,J=["timestamp","level","logger","message","data"],V=class{constructor(){o(this,"name",D),o(this,"version",N),o(this,"kind",C),o(this,"_config"),o(this,"_format"),o(this,"_pretty"),o(this,"_maxDepth",3),o(this,"_maxStringLen",1e3),o(this,"_maxArrayLen",100),o(this,"_timestampFormat","iso")}init(t){var e,i,s,r,n;this.t=Object.assign({},t),this.A=!0===this.t.pretty?2:this.t.pretty?this.t.pretty:void 0,this.F=null!=(e=this.t.maxDepth)?e:3,this.$=null!=(i=this.t.maxStringLen)?i:1e3,this.P=null!=(s=this.t.maxArrayLen)?s:100;let o=null!=(r=this.t.format)?r:J;"string"==typeof this.t.format&&(o=[this.t.format]),this.L=o,this.j=null!=(n=this.t.timestampFormat)?n:"iso"}dispose(){}format(t){let e={},i=this.L;return i.length>0?i.forEach(i=>{let s=this.resolveToken(i,t);null!=s&&(e[s.key]=s.value)}):e=t,[_.stringifyLog(e,{maxDepth:this.F,maxStringLength:this.$,maxArrayLength:this.P},this.A)]}resolveToken(t,e){let i=t;if("LEVEL"===i)return{key:i,value:e.level};if("timestamp"===i){let t=_.getPropertyByPath(e,i);return{key:i,value:_.formatTimestamp(t,this.j)}}return{key:i,value:_.getPropertyByPath(e,i)}}},B=class{constructor(){o(this,"name",D),o(this,"version",N),o(this,"kind",C)}create(t){let e=new V;return e.init(t),e}},T=class{constructor(){o(this,"_pluginFactories",new Map),o(this,"_plugins",[])}registerPluginFactory(t){if(this.q.has(t.name))throw new Error(`LogM8: Plugin with name ${t.name} is already registered.`);this.q.set(t.name,t)}createPlugin(t,e){let i="string"==typeof e?e:e.name,s="string"==typeof e?{name:i}:e,r=this.getPluginFactory(i,t);if(!r)throw new Error(`LogM8: Plugin factory kind '${t}' with name '${i}' not found.`);let n=r.create(s);return this.D.push(n),n}getPluginFactory(t,e){let i=this.q.get(t);if(i&&e===i.kind)return i}disposePlugins(){this.D.forEach(t=>{t.dispose()}),this.D=[]}clearFactories(){this.q.clear()}},W=[{name:"console",formatter:"default-formatter"}],K=new class{constructor(){o(this,"_initialized",!1),o(this,"_pluginManager",new T),o(this,"_loggers",new Map),o(this,"_appenders",[]),o(this,"_filters",[]),o(this,"_defaultLevel",h.info),o(this,"_logLevelValues",Object.values(h)),o(this,"_logLevelSet",new Set(this.N)),o(this,"_logBuffer",[]),this.C.registerPluginFactory(new m),this.C.registerPluginFactory(new O),this.C.registerPluginFactory(new q),this.C.registerPluginFactory(new B),this.C.registerPluginFactory(new A)}init(t){var e,i,s,r;t=Object.assign({},t),this.J(),this.V=this.B.has(t.level)?t.level:h.info;for(let[i,s]of Object.entries(null!=(e=t.loggers)?e:{})){let t=this.getLogger(i),e=this.B.has(s)?s:this.V;t.setLevel(e)}let n=null!=(i=t.appenders)?i:W;for(let t of n){let e=this.C.createPlugin(a.appender,t),i=t.formatter?this.C.createPlugin(a.formatter,t.formatter):void 0,r=[],n=t;for(let e of null!=(s=n.filters)?s:[]){let i=this.C.createPlugin(a.filter,e);i?r.push(i):console&&console.log&&console.log(`LogM8: Filter '${e}' not found for appender ${t.name}.`)}e.init(t,i,r),this.T.push(e)}this.W();for(let e of null!=(r=t.filters)?r:[]){let t=this.C.createPlugin(a.filter,e);t?this.o.push(t):console&&console.log&&console.log(`LogM8: Filter '${e}' not found (global).`)}this.K=!0}dispose(){this.J(),this.R=[],this.C.clearFactories(),this.K=!1}getLogger(t){let e=t;Array.isArray(t)&&(e=t.join("."));let i=this.U.get(e);if(i)return i;let s={name:e,level:this.V,context:{}};return s.fatal=this.G.bind(this,s,h.fatal),s.error=this.G.bind(this,s,h.error),s.warn=this.G.bind(this,s,h.warn),s.info=this.G.bind(this,s,h.info),s.debug=this.G.bind(this,s,h.debug),s.trace=this.G.bind(this,s,h.trace),s.track=this.G.bind(this,s,h.track),s.setLevel=this.H.bind(this,s),s.setContext=this.I.bind(this,s),s.getLogger=t=>this.getLogger([s.name,t]),this.H(s,this.V),this.U.set(s.name,s),s}enableAppender(t){let e=this.X(t);e&&(e.enabled=!0)}disableAppender(t){let e=this.X(t);e&&(e.enabled=!1)}flushAppender(t){let e=this.X(t);if(e)try{e.flush()}catch(t){console&&console.error&&console.error(`LogM8: Failed to flush appender: ${e.name}:`,t)}}flushAppenders(){for(let t of this.T)this.flushAppender(t.name)}enableFilter(t,e){var i;if(e)return void(null==(i=this.X(e))||i.enableFilter(t));let s=this.h(t);s&&(s.enabled=!0)}disableFilter(t,e){var i;if(e)return void(null==(i=this.X(e))||i.disableFilter(t));let s=this.h(t);s&&(s.enabled=!1)}registerPluginFactory(t){this.C.registerPluginFactory(t)}G(t,e,i,...s){if(this.N.indexOf(e)>t.Y)return;let r={logger:t.name,level:e,message:i,data:s,context:t.context,timestamp:new Date};if(this.K){if(this.R.length>0){for(let t of this.R)this.Z(t);this.R=[]}this.Z(r)}else this.R.length>=100&&this.R.shift(),this.R.push(r)}H(t,e){t.level=e,t.Y=this.N.indexOf(e),t.isEnabled=e!==h.off;let i=t.Y;t.isFatal=this.N.indexOf(h.fatal)<=i,t.isError=this.N.indexOf(h.error)<=i,t.isWarn=this.N.indexOf(h.warn)<=i,t.isInfo=this.N.indexOf(h.info)<=i,t.isDebug=this.N.indexOf(h.debug)<=i,t.isTrack=this.N.indexOf(h.track)<=i,t.isTrace=this.N.indexOf(h.trace)<=i}I(t,e){t.context=null!=e?e:{}}Z(t){for(let e of this.o)if(e.enabled&&!e.filter(t))return;for(let e of this.T)try{if(!e.enabled||!e.supportedLevels.has(t.level))continue;e.write(t)}catch(t){console&&console.log&&console.log(`LogM8: Failed to append log with '${e.name}':`,t)}}X(t){return this.T.find(e=>e.name===t)}W(){this.T.sort((t,e)=>{var i,s;let r=null!=(i=null==t?void 0:t.priority)?i:0;return(null!=(s=null==e?void 0:e.priority)?s:0)-r})}h(t){return this.o.find(e=>e.name===t)}J(){this.flushAppenders(),this.T=[],this.U.clear(),this.V=h.info,this.C.disposePlugins()}};return(t=>((t,n,o,l)=>{if(n&&"object"==typeof n||"function"==typeof n)for(let h of s(n))!r.call(t,h)&&h!==o&&e(t,h,{get:()=>n[h],enumerable:!(l=i(n,h))||l.enumerable});return t})(e({},"__esModule",{value:!0}),t))(l)})();//# sourceMappingURL=log-m8.global.js.map
1
+ "use strict";var logM8=(()=>{var e=Object.defineProperty,t=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,s=Object.prototype.hasOwnProperty,i=(t,r,s)=>((t,r,s)=>r in t?e(t,r,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[r]=s)(t,"symbol"!=typeof r?r+"":r,s),n={};((t,r)=>{for(var s in r)e(t,s,{get:r[s],enumerable:!0})})(n,{LogLevel:()=>l,LogM8:()=>$,LogM8Utils:()=>b,PluginKind:()=>a});var o,l={off:"off",fatal:"fatal",error:"error",warn:"warn",info:"info",debug:"debug",track:"track",trace:"trace"},a={appender:"appender",filter:"filter",formatter:"formatter"},c="console",h="1.0.0",g=a.appender,f=new Set([l.fatal,l.error,l.warn,l.info,l.debug,l.track,l.trace]),u=class{constructor(){i(this,"name",c),i(this,"version",h),i(this,"kind",g),i(this,"supportedLevels",f),i(this,"enabled",!0),i(this,"priority"),i(this,"_config"),i(this,"_formatter"),i(this,"_filters",[]),i(this,"_available",!0),i(this,"off",()=>{}),i(this,"fatal",console.error?console.error.bind(console):console.log.bind(console)),i(this,"error",console.error?console.error.bind(console):console.log.bind(console)),i(this,"warn",console.warn?console.warn.bind(console):console.log.bind(console)),i(this,"info",console.info?console.info.bind(console):console.log.bind(console)),i(this,"debug",console.debug?console.debug.bind(console):console.log.bind(console)),i(this,"trace",console.debug?console.debug.bind(console):console.log.bind(console)),i(this,"track",console.log.bind(console))}init(e,t,r){var s,i;this._config=e,this._formatter=t,this._filters=r||[],this._available="undefined"!=typeof console&&!!console.log,this.enabled=!1!==(null==(s=this._config)?void 0:s.enabled),this.priority=null==(i=this._config)?void 0:i.priority}dispose(){}write(e){if(!this._available)return;for(const t of this._filters)if(t.enabled&&!t.filter(e))return;const t=this._formatter?this._formatter.format(e):[e];this[e.level](...t)}flush(){}enableFilter(e){const t=this._getFilter(e);t&&(t.enabled=!0)}disableFilter(e){const t=this._getFilter(e);t&&(t.enabled=!1)}_getFilter(e){return this._filters.find(t=>t.name===e)}},_=class{constructor(){i(this,"name",c),i(this,"version",h),i(this,"kind",g)}create(e){const t=new u;return t.init(e),t}},p=/(yyyy|SSS|hh|mm|ss|SS|zz|z|yy|MM|dd|A|a|h|S)/g,d=new Set(["name","message","stack","cause"]),m=["code","errno","syscall","path"],b=class e{static isBrowser(){return"undefined"!=typeof window&&void 0!==window.document}static isString(e){return"string"==typeof e||e instanceof String}static getPropertyByPath(e,t){let r=e;const s=t.replace(/\[(\d+)\]/g,".$1").split(".");for(const e of s){if("object"!=typeof r||null===r)return;if(Array.isArray(r)){const t=Number(e);if(Number.isInteger(t)&&t>=0){r=r[t];continue}}r=r[e]}return r}static formatTimestamp(e,t){const r=null==t?void 0:t.toLowerCase();if(!t||"iso"===r||"toisostring"===r)return e.toISOString();if("locale"===r||"tolocalestring"===r)return e.toLocaleString();const s=(e,t=2)=>String(e).padStart(t,"0"),i=e.getHours(),n=i%12==0?12:i%12;return t.replace(p,t=>{switch(t){case"yyyy":return s(e.getFullYear(),4);case"yy":return s(e.getFullYear()%100);case"MM":return s(e.getMonth()+1);case"dd":return s(e.getDate());case"hh":return s(i);case"h":return s(n);case"mm":return s(e.getMinutes());case"ss":return s(e.getSeconds());case"SSS":return s(e.getMilliseconds(),3);case"SS":return s(Math.floor(e.getMilliseconds()/10),2);case"S":return s(Math.floor(e.getMilliseconds()/100),1);case"A":return i<12?"AM":"PM";case"a":return i<12?"am":"pm";case"z":case"zz":{const r=-e.getTimezoneOffset(),i=r>=0?"+":"-",n=Math.floor(Math.abs(r)/60),o=Math.abs(r)%60;return"z"===t?`${i}${s(n)}:${s(o)}`:`${i}${s(n)}${s(o)}`}default:return t}})}static stringifyLog(t,{maxDepth:r=3,maxStringLength:s=200,maxArrayLength:i=100}={},n){const o=new WeakMap;return JSON.stringify(t,function(t,n){var l;if(n&&"object"==typeof n){const e=null!=(l=o.get(this))?l:0;if(e>=r)return Array.isArray(n)?"[Array]":"[Object]";if(o.set(n,e+1),Array.isArray(n)&&n.length>i){const e=n.slice(0,i);return e.push(`... ${n.length-i} more items`),e}}return e.isString(n)&&n.length>s?n.slice(0,s)+"…":"bigint"==typeof n?n.toString():n instanceof Date?n.toISOString():n instanceof Error?e.serializeError(n):n},n)}static serializeError(e){const t=(e,r)=>{if(!e)return null;const s=e;if("object"==typeof s&&null!==s){if(r.has(s))return{name:"CircularReference",message:"Circular reference detected in error cause chain"};r.add(s)}if("function"==typeof s.toJSON)try{const e=s.toJSON();return JSON.stringify(e),e}catch(e){}const i={name:s.name||"Error",message:s.message||"",stack:s.stack};"cause"in s&&void 0!==s.cause&&(i.cause=t(s.cause,r));for(const e in s)if(Object.prototype.hasOwnProperty.call(s,e)&&!d.has(e))try{const t=s[e];JSON.stringify(t),i[e]=t}catch(e){}for(const e of m)try{const t=Object.getOwnPropertyDescriptor(s,e);t&&void 0!==t.value&&(JSON.stringify(t.value),i[e]=t.value)}catch(e){}return i};return t(e,new WeakSet)}},y=class{constructor(){i(this,"name","match-filter"),i(this,"version","1.0.0"),i(this,"kind",a.filter),i(this,"enabled",!0),i(this,"_allow"),i(this,"_deny")}init(e){var t,r;const s=null!=e?e:{};this._allow=null!=(t=s.allow)?t:void 0,this._deny=null!=(r=s.deny)?r:void 0,this.enabled=!1!==s.enabled}dispose(){}filter(e){try{if(this._allow&&Object.keys(this._allow).length>0)for(const[t,r]of Object.entries(this._allow)){const s=b.getPropertyByPath(e,t);if(!this._isEqual(s,r))return!1}if(this._deny&&Object.keys(this._deny).length>0)for(const[t,r]of Object.entries(this._deny)){const s=b.getPropertyByPath(e,t);if(this._isEqual(s,r))return!1}return!0}catch(e){return!1}}_isEqual(e,t){if(e===t)return!0;if("number"==typeof e&&"number"==typeof t)return Number.isNaN(e)&&Number.isNaN(t);if(e instanceof Date&&t instanceof Date)return e.getTime()===t.getTime();if(Array.isArray(e)&&Array.isArray(t)){if(e.length!==t.length)return!1;for(let r=0;r<e.length;r++)if(!this._isEqual(e[r],t[r]))return!1;return!0}if(this._isPlainObject(e)&&this._isPlainObject(t)){const r=Object.keys(e),s=Object.keys(t);if(r.length!==s.length)return!1;for(const s of r){if(!Object.prototype.hasOwnProperty.call(t,s))return!1;if(!this._isEqual(e[s],t[s]))return!1}return!0}return!1}_isPlainObject(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)&&Object.getPrototypeOf(e)===Object.prototype}},v=class{constructor(){i(this,"name","match-filter"),i(this,"version","1.0.0"),i(this,"kind",a.filter)}create(e){const t=new y;return t.init(e),t}},L="default-formatter",w="1.0.0",O=a.formatter,M=["{timestamp} {LEVEL} [{logger}]","{message}","{data}"],S="hh:mm:ss.SSS",P=class{constructor(){i(this,"name",L),i(this,"version",w),i(this,"kind",O),i(this,"_config"),i(this,"_format"),i(this,"_timestampFormat",S),i(this,"_levelMap"),i(this,"_colorEnabled",!1),i(this,"_levelColorMap",{trace:"",track:"",debug:"",info:"",warn:"",error:"",fatal:""}),i(this,"_levelCssColorMap",{trace:"color: #bbb;",track:"color: orange;",debug:"color: grey;",info:"color: blue;",warn:"color: gold;",error:"color: red;",fatal:"background: red; color: white;"})}init(e){var t,r;const s=b.isBrowser();this._config=Object.assign({},e),this._colorEnabled=!!this._config.color,this._format=[];let i=null!=(t=this._config.format)?t:M;if("string"==typeof this._config.format&&(i=[this._config.format]),i)for(const e of i){const t=/(\{[^}]+\})|([^{}]+)/g,r=[];let s;for(;null!==(s=t.exec(e));)s[1]?r.push(s[1]):void 0!==s[2]&&r.push(s[2]);this._format.push(r)}this._timestampFormat=null!=(r=this._config.timestampFormat)?r:S;const n=Object.values(l),o=Math.max(...n.map(e=>e.length));this._levelMap=n.reduce((e,t)=>{let r=t.toUpperCase().padEnd(o," ");if(this._colorEnabled){if(s){const s=this._levelCssColorMap[t]||"";return e[t]=[`%c${r}`,s],e}r=(this._levelColorMap[t]||"")+r+""}return e[t]=r,e},{})}dispose(){}format(e){let t;const r=this._format;if(r.length>0){t=r.map(t=>1===t.length?this.resolveToken(t[0],e):t.reduce((t,r)=>{const s=this.resolveToken(r,e);return t+String(s)},""));const s=t.findIndex(e=>Array.isArray(e));if(s>=0){const e=t[s];e.length>0?t.splice(s,1,...e):t.splice(s,1)}}else t=[b.formatTimestamp(e.timestamp,this._timestampFormat),e.level,e.logger,e.message,...e.data,e.context];return t}resolveToken(e,t){var r;if(e.startsWith("{")&&e.endsWith("}")){const s=e.slice(1,-1);if("message"===s&&"string"!=typeof t.message)return t.message;if("LEVEL"===s)return null!=(r=this._levelMap[t.level])?r:t.level;if("timestamp"===s){const e=b.getPropertyByPath(t,s);return b.formatTimestamp(e,this._timestampFormat)}return b.getPropertyByPath(t,s)}return e}},F=class{constructor(){i(this,"name",L),i(this,"version",w),i(this,"kind",O)}create(e){const t=new P;return t.init(e),t}},x="json-formatter",A="1.0.0",k=a.formatter,j=["timestamp","level","logger","message","data"],E=class{constructor(){i(this,"name",x),i(this,"version",A),i(this,"kind",k),i(this,"_config"),i(this,"_format"),i(this,"_pretty"),i(this,"_maxDepth",3),i(this,"_maxStringLen",1e3),i(this,"_maxArrayLen",100),i(this,"_timestampFormat","iso")}init(e){var t,r,s,i,n;this._config=Object.assign({},e),this._pretty=!0===this._config.pretty?2:this._config.pretty?this._config.pretty:void 0,this._maxDepth=null!=(t=this._config.maxDepth)?t:3,this._maxStringLen=null!=(r=this._config.maxStringLen)?r:1e3,this._maxArrayLen=null!=(s=this._config.maxArrayLen)?s:100;let o=null!=(i=this._config.format)?i:j;"string"==typeof this._config.format&&(o=[this._config.format]),this._format=o,this._timestampFormat=null!=(n=this._config.timestampFormat)?n:"iso"}dispose(){}format(e){let t={};const r=this._format;return r.length>0?r.forEach(r=>{const s=this.resolveToken(r,e);null!=s&&(t[s.key]=s.value)}):t=e,[b.stringifyLog(t,{maxDepth:this._maxDepth,maxStringLength:this._maxStringLen,maxArrayLength:this._maxArrayLen},this._pretty)]}resolveToken(e,t){const r=e;if("LEVEL"===r)return{key:r,value:t.level};if("timestamp"===r){const e=b.getPropertyByPath(t,r);return{key:r,value:b.formatTimestamp(e,this._timestampFormat)}}return{key:r,value:b.getPropertyByPath(t,r)}}},N=class{constructor(){i(this,"name",x),i(this,"version",A),i(this,"kind",k)}create(e){const t=new E;return t.init(e),t}},B=class{constructor(){i(this,"_pluginFactories",new Map),i(this,"_plugins",[])}registerPluginFactory(e){if(this._pluginFactories.has(e.name))throw new Error(`LogM8: Plugin with name ${e.name} is already registered.`);this._pluginFactories.set(e.name,e)}createPlugin(e,t){const r="string"==typeof t?t:t.name,s="string"==typeof t?{name:r}:t,i=this.getPluginFactory(r,e);if(!i)throw new Error(`LogM8: Plugin factory kind '${e}' with name '${r}' not found.`);const n=i.create(s);return this._plugins.push(n),n}getPluginFactory(e,t){const r=this._pluginFactories.get(e);if(r&&t===r.kind)return r}disposePlugins(){this._plugins.forEach(e=>{e.dispose()}),this._plugins=[]}clearFactories(){this._pluginFactories.clear()}},V=[{name:"console",formatter:"default-formatter"}],$=new class{constructor(){i(this,"_initialized"),i(this,"_pluginManager"),i(this,"_loggers"),i(this,"_appenders"),i(this,"_filters",[]),i(this,"_globalLogLevel"),i(this,"_globalLogLevelNumber"),i(this,"_logLevelValues"),i(this,"_logLevelSet"),i(this,"_logBuffer"),this._initialized=!1,this._pluginManager=new B,this._loggers=new Map,this._appenders=[],this._filters=[],this._logLevelValues=Object.values(l),this._logLevelSet=new Set(this._logLevelValues),this._globalLogLevel=l.info,this._globalLogLevelNumber=this._logLevelValues.indexOf(l.info),this._logBuffer=[],this._pluginManager.registerPluginFactory(new _),this._pluginManager.registerPluginFactory(new F),this._pluginManager.registerPluginFactory(new N),this._pluginManager.registerPluginFactory(new v)}init(e){var t,r,s,i;e=Object.assign({},e),this._reset(),this._globalLogLevel=this._logLevelSet.has(e.level)?e.level:l.info;for(const[r,s]of Object.entries(null!=(t=e.loggers)?t:{})){const e=this.getLogger(r),t=this._logLevelSet.has(s)?s:this._globalLogLevel;e.setLevel(t)}const n=null!=(r=e.appenders)?r:V;for(const e of n){const t=this._pluginManager.createPlugin(a.appender,e),r=e.formatter?this._pluginManager.createPlugin(a.formatter,e.formatter):void 0,i=[],n=e;for(const t of null!=(s=n.filters)?s:[]){const r=this._pluginManager.createPlugin(a.filter,t);r?i.push(r):console&&console.log&&console.log(`LogM8: Filter '${t}' not found for appender ${e.name}.`)}t.init(e,r,i),this._appenders.push(t)}this._sortAppenders();for(const t of null!=(i=e.filters)?i:[]){const e=this._pluginManager.createPlugin(a.filter,t);e?this._filters.push(e):console&&console.log&&console.log(`LogM8: Filter '${t}' not found (global).`)}this._initialized=!0}dispose(){this._reset(),this._logBuffer=[],this._pluginManager.clearFactories(),this._initialized=!1}getLogger(e){let t=e;Array.isArray(e)&&(t=e.join("."));const r=this._loggers.get(t);if(r)return r;const s={name:t,level:this._globalLogLevel,context:{}};return s.fatal=this._log.bind(this,s,l.fatal),s.error=this._log.bind(this,s,l.error),s.warn=this._log.bind(this,s,l.warn),s.info=this._log.bind(this,s,l.info),s.debug=this._log.bind(this,s,l.debug),s.trace=this._log.bind(this,s,l.trace),s.track=this._log.bind(this,s,l.track),s.setLevel=this._setLevel.bind(this,s),s.setContext=this._setContext.bind(this,s),s.getLogger=e=>this.getLogger([s.name,e]),this._setLevel(s,this._globalLogLevel),this._loggers.set(s.name,s),s}setLevel(e,t){t?this.getLogger(t).setLevel(e):(this._globalLogLevel=this._logLevelSet.has(e)?e:this._globalLogLevel,this._globalLogLevelNumber=this._logLevelValues.indexOf(this._globalLogLevel))}enableAppender(e){const t=this._getAppender(e);t&&(t.enabled=!0)}disableAppender(e){const t=this._getAppender(e);t&&(t.enabled=!1)}flushAppender(e){const t=this._getAppender(e);if(t)try{t.flush()}catch(e){console&&console.error&&console.error(`LogM8: Failed to flush appender: ${t.name}:`,e)}}flushAppenders(){for(const e of this._appenders)this.flushAppender(e.name)}enableFilter(e,t){var r;if(t)return void(null==(r=this._getAppender(t))||r.enableFilter(e));const s=this._getFilter(e);s&&(s.enabled=!0)}disableFilter(e,t){var r;if(t)return void(null==(r=this._getAppender(t))||r.disableFilter(e));const s=this._getFilter(e);s&&(s.enabled=!1)}registerPluginFactory(e){this._pluginManager.registerPluginFactory(e)}_log(e,t,r,...s){const i=this._logLevelValues.indexOf(t);if(i>e._levelNumber||i>this._globalLogLevelNumber)return;const n={logger:e.name,level:t,message:r,data:s,context:e.context,timestamp:new Date};if(this._initialized){if(this._logBuffer.length>0){for(const e of this._logBuffer)this._processLogEvent(e);this._logBuffer=[]}this._processLogEvent(n)}else this._logBuffer.length>=100&&this._logBuffer.shift(),this._logBuffer.push(n)}_setLevel(e,t){e.level=this._logLevelSet.has(t)?t:e.level,e._levelNumber=this._logLevelValues.indexOf(t),e.isEnabled=e.level!==l.off;const r=e._levelNumber;e.isFatal=this._logLevelValues.indexOf(l.fatal)<=r,e.isError=this._logLevelValues.indexOf(l.error)<=r,e.isWarn=this._logLevelValues.indexOf(l.warn)<=r,e.isInfo=this._logLevelValues.indexOf(l.info)<=r,e.isDebug=this._logLevelValues.indexOf(l.debug)<=r,e.isTrack=this._logLevelValues.indexOf(l.track)<=r,e.isTrace=this._logLevelValues.indexOf(l.trace)<=r}_setContext(e,t){e.context=null!=t?t:{}}_processLogEvent(e){for(const t of this._filters)if(t.enabled&&!t.filter(e))return;for(const t of this._appenders)try{if(!t.enabled)continue;if(!t.supportedLevels.has(e.level))continue;t.write(e)}catch(e){console&&console.log&&console.log(`LogM8: Failed to append log with '${t.name}':`,e)}}_getAppender(e){return this._appenders.find(t=>t.name===e)}_sortAppenders(){this._appenders.sort((e,t)=>{var r,s;const i=null!=(r=null==e?void 0:e.priority)?r:0;return(null!=(s=null==t?void 0:t.priority)?s:0)-i})}_getFilter(e){return this._filters.find(t=>t.name===e)}_reset(){this.flushAppenders(),this._appenders=[],this._loggers.clear(),this._globalLogLevel=l.info,this._pluginManager.disposePlugins()}};return o=n,((i,n,o,l)=>{if(n&&"object"==typeof n||"function"==typeof n)for(let a of r(n))s.call(i,a)||a===o||e(i,a,{get:()=>n[a],enumerable:!(l=t(n,a))||l.enumerable});return i})(e({},"__esModule",{value:!0}),o)})();//# sourceMappingURL=log-m8.global.js.map