@ncoderz/log-m8 1.2.1 → 1.2.2

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
@@ -101,6 +101,7 @@ LogM8.init({
101
101
  name: 'console',
102
102
  formatter: {
103
103
  name: 'default-formatter',
104
+ format: ['{timestamp} {LEVEL} [{logger}]', '{message}', '{data}'],
104
105
  timestampFormat: 'yyyy-MM-dd hh:mm:ss.SSS',
105
106
  color: true
106
107
  },
@@ -110,7 +111,11 @@ LogM8.init({
110
111
  {
111
112
  name: 'file',
112
113
  filename: 'app.log',
113
- formatter: 'json-formatter',
114
+ formatter: {
115
+ name: 'json-formatter',
116
+ format: ['timestamp', 'level', 'logger', 'message', 'data'],
117
+ timestampFormat: 'iso',
118
+ }
114
119
  }
115
120
  ],
116
121
 
@@ -164,13 +169,13 @@ Formatters transform log events into output formats suitable for different appen
164
169
 
165
170
  ### Supported Formatter Tokens
166
171
 
167
- - `{timestamp}`: Formatted timestamp
168
- - `{LEVEL}`: Uppercase level label (with optional colorization)
169
- - `{level}`: Lowercase level name
170
- - `{logger}`: Logger name
171
- - `{message}`: Primary log message
172
- - `{data}`: Additional data arguments
173
- - `{context.*}`: Nested context properties
172
+ - `timestamp`: Formatted timestamp
173
+ - `LEVEL`: Uppercase level label (Default Formatter Only. With standard padding, and optional colorization)
174
+ - `level`: Lowercase level name
175
+ - `logger`: Logger name
176
+ - `message`: Primary log message
177
+ - `data`: Additional data arguments
178
+ - `context.*`: Nested context properties
174
179
 
175
180
  All tokens support accessing nested items with `data[0].property` like notation.
176
181
 
@@ -1 +1 @@
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:()=>V,LogM8Utils:()=>y,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)}},p=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}},_=/(yyyy|SSS|hh|mm|ss|SS|zz|z|yy|MM|dd|A|a|h|S)/g,d=/^\/(.+)\/([dgimsuvy]*)$/,m=new Set(["name","message","stack","cause"]),b=["code","errno","syscall","path"],y=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(_,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)&&!m.has(e))try{const t=s[e];JSON.stringify(t),i[e]=t}catch(e){}for(const e of b)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)}static parseRegexFromString(e,t){try{const r=e.match(d);if(null==r)return;const[,s,i]=r;let n=i;if(Array.isArray(t))for(const e of t)i.includes(e)||(n+=e);return new RegExp(s,n)}catch(e){return}}},v=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){const t=null!=e?e:{};this._allow=this._prepareRules(t.allow),this._deny=this._prepareRules(t.deny),this.enabled=!1!==t.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=y.getPropertyByPath(e,t);if(!this._matches(s,r))return!1}if(this._deny&&Object.keys(this._deny).length>0)for(const[t,r]of Object.entries(this._deny)){const s=y.getPropertyByPath(e,t);if(this._matches(s,r))return!1}return!0}catch(e){return!1}}_matches(e,t){if(t instanceof RegExp){if(null==e)return!1;const r="string"==typeof e?e:String(e);return t.test(r)}return this._isEqual(e,t)}_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}_prepareRules(e){if(!e||0===Object.keys(e).length)return;const t={};for(const[r,s]of Object.entries(e))if("string"==typeof s){const e=y.parseRegexFromString(s);t[r]=null!=e?e:s}else t[r]=s;return t}},L=class{constructor(){i(this,"name","match-filter"),i(this,"version","1.0.0"),i(this,"kind",a.filter)}create(e){const t=new v;return t.init(e),t}},w="default-formatter",O="1.0.0",S=a.formatter,M=["{timestamp} {LEVEL} [{logger}]","{message}","{data}"],F="hh:mm:ss.SSS",P=class{constructor(){i(this,"name",w),i(this,"version",O),i(this,"kind",S),i(this,"_config"),i(this,"_format"),i(this,"_timestampFormat",F),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=y.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:F;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=[y.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=y.getPropertyByPath(t,s);return y.formatTimestamp(e,this._timestampFormat)}return y.getPropertyByPath(t,s)}return e}},x=class{constructor(){i(this,"name",w),i(this,"version",O),i(this,"kind",S)}create(e){const t=new P;return t.init(e),t}},A="json-formatter",k="1.0.0",j=a.formatter,E=["timestamp","level","logger","message","data"],N=class{constructor(){i(this,"name",A),i(this,"version",k),i(this,"kind",j),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:E;"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,[y.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=y.getPropertyByPath(t,r);return{key:r,value:y.formatTimestamp(e,this._timestampFormat)}}return{key:r,value:y.getPropertyByPath(t,r)}}},B=class{constructor(){i(this,"name",A),i(this,"version",k),i(this,"kind",j)}create(e){const t=new N;return t.init(e),t}},$=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()}},z=[{name:"console",formatter:"default-formatter"}],V=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 $,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 p),this._pluginManager.registerPluginFactory(new x),this._pluginManager.registerPluginFactory(new B),this._pluginManager.registerPluginFactory(new L)}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:z;for(const e of n){const t=this._pluginManager.createPlugin(a.appender,e),r=y.isString(e)?{name:t.name}:e,i=(null==r?void 0:r.formatter)?this._pluginManager.createPlugin(a.formatter,r.formatter):void 0,n=[],o=r;for(const e of null!=(s=o.filters)?s:[]){const t=this._pluginManager.createPlugin(a.filter,e);t?n.push(t):console&&console.log&&console.log(`LogM8: Filter '${e}' not found for appender ${r.name}.`)}t.init(r,i,n),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}isInitialized(){return this._initialized}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
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:()=>V,LogM8Utils:()=>y,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)}},p=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}},_=/(yyyy|SSS|hh|mm|ss|SS|zz|z|yy|MM|dd|A|a|h|S)/g,d=/^\/(.+)\/([dgimsuvy]*)$/,m=new Set(["name","message","stack","cause"]),b=["code","errno","syscall","path"],y=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(_,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)&&!m.has(e))try{const t=s[e];JSON.stringify(t),i[e]=t}catch(e){}for(const e of b)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)}static parseRegexFromString(e,t){try{const r=e.match(d);if(null==r)return;const[,s,i]=r;let n=i;if(Array.isArray(t))for(const e of t)i.includes(e)||(n+=e);return new RegExp(s,n)}catch(e){return}}},v=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){const t=null!=e?e:{};this._allow=this._prepareRules(t.allow),this._deny=this._prepareRules(t.deny),this.enabled=!1!==t.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=y.getPropertyByPath(e,t);if(!this._matches(s,r))return!1}if(this._deny&&Object.keys(this._deny).length>0)for(const[t,r]of Object.entries(this._deny)){const s=y.getPropertyByPath(e,t);if(this._matches(s,r))return!1}return!0}catch(e){return!1}}_matches(e,t){if(t instanceof RegExp){if(null==e)return!1;const r="string"==typeof e?e:String(e);return t.test(r)}return this._isEqual(e,t)}_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}_prepareRules(e){if(!e||0===Object.keys(e).length)return;const t={};for(const[r,s]of Object.entries(e))if("string"==typeof s){const e=y.parseRegexFromString(s);t[r]=null!=e?e:s}else t[r]=s;return t}},L=class{constructor(){i(this,"name","match-filter"),i(this,"version","1.0.0"),i(this,"kind",a.filter)}create(e){const t=new v;return t.init(e),t}},w="default-formatter",O="1.0.0",S=a.formatter,M=["{timestamp} {LEVEL} [{logger}]","{message}","{data}"],F="hh:mm:ss.SSS",P=class{constructor(){i(this,"name",w),i(this,"version",O),i(this,"kind",S),i(this,"_config"),i(this,"_format"),i(this,"_timestampFormat",F),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=y.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:F;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=[y.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=y.getPropertyByPath(t,s);return y.formatTimestamp(e,this._timestampFormat)}return y.getPropertyByPath(t,s)}return e}},x=class{constructor(){i(this,"name",w),i(this,"version",O),i(this,"kind",S)}create(e){const t=new P;return t.init(e),t}},A="json-formatter",k="1.0.0",j=a.formatter,E=["timestamp","level","logger","message","data"],N=class{constructor(){i(this,"name",A),i(this,"version",k),i(this,"kind",j),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:E;"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,[y.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=y.getPropertyByPath(t,r);return{key:r,value:y.formatTimestamp(e,this._timestampFormat)}}return{key:r,value:y.getPropertyByPath(t,r)}}},B=class{constructor(){i(this,"name",A),i(this,"version",k),i(this,"kind",j)}create(e){const t=new N;return t.init(e),t}},$=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()}},z=[{name:"console",formatter:"default-formatter"}],V=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 $,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 p),this._pluginManager.registerPluginFactory(new x),this._pluginManager.registerPluginFactory(new B),this._pluginManager.registerPluginFactory(new L)}init(e){var t,r,s,i,n;e=Object.assign({},e),this._reset();const o=(null!=(t=e.level)?t:"").trim().toLowerCase();this._globalLogLevel=this._logLevelSet.has(o)?o:l.info;for(const[t,s]of Object.entries(null!=(r=e.loggers)?r:{})){const e=(null!=s?s:"").trim().toLowerCase(),r=this.getLogger(t),i=this._logLevelSet.has(e)?e:this._globalLogLevel;r.setLevel(i)}const c=null!=(s=e.appenders)?s:z;for(const e of c){const t=this._pluginManager.createPlugin(a.appender,e),r=y.isString(e)?{name:t.name}:e,s=(null==r?void 0:r.formatter)?this._pluginManager.createPlugin(a.formatter,r.formatter):void 0,n=[],o=r;for(const e of null!=(i=o.filters)?i:[]){const t=this._pluginManager.createPlugin(a.filter,e);t?n.push(t):console&&console.log&&console.log(`LogM8: Filter '${e}' not found for appender ${r.name}.`)}t.init(r,s,n),this._appenders.push(t)}this._sortAppenders();for(const t of null!=(n=e.filters)?n:[]){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}isInitialized(){return this._initialized}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