@ncoderz/log-m8 1.2.4 → 1.3.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 +33 -1
- package/dist/browser/log-m8.global.js +1 -1
- package/dist/browser/log-m8.global.js.map +1 -1
- package/dist/index.cjs +164 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +408 -55
- package/dist/index.d.ts +408 -55
- package/dist/index.js +158 -39
- package/dist/index.js.map +1 -1
- package/package.json +13 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# log-m8
|
|
2
2
|
|
|
3
|
-

|
|
4
4
|

|
|
5
5
|

|
|
6
6
|
|
|
@@ -12,6 +12,7 @@ A fast, small, flexible and extensible logging system for TypeScript and JavaScr
|
|
|
12
12
|
|
|
13
13
|
- 🌲 **Hierarchical loggers** - organize by module with inheritance
|
|
14
14
|
- 🔌 **Plugin architecture** - extensible with custom appenders, formatters, and filters
|
|
15
|
+
- 🔄 **Dynamic appenders** - add and remove appenders at runtime without re-initialization
|
|
15
16
|
- 🎨 **Multiple output targets** - console, file, and more with custom formatters
|
|
16
17
|
- 🔍 **Configurable filters** - control what gets logged and where
|
|
17
18
|
- 🚀 **Performance-optimized** - designed for minimal overhead
|
|
@@ -264,6 +265,37 @@ LogM8.enableFilter('sensitive-data', 'console');
|
|
|
264
265
|
|
|
265
266
|
```
|
|
266
267
|
|
|
268
|
+
### Dynamic Appender Management
|
|
269
|
+
|
|
270
|
+
Appenders can be added and removed after initialization.
|
|
271
|
+
This is useful for diagnostic injection, feature-flag-driven logging, or attaching
|
|
272
|
+
test spies at runtime.
|
|
273
|
+
|
|
274
|
+
```typescript
|
|
275
|
+
// Add an appender at runtime
|
|
276
|
+
LogM8.addAppender({
|
|
277
|
+
name: 'file',
|
|
278
|
+
filename: 'debug.log',
|
|
279
|
+
formatter: 'json-formatter',
|
|
280
|
+
priority: 10
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// Add by name (uses factory defaults)
|
|
284
|
+
LogM8.addAppender('console');
|
|
285
|
+
|
|
286
|
+
// Check which appenders are active (returns names in priority order)
|
|
287
|
+
console.log(LogM8.getAppenderNames()); // ['file', 'console']
|
|
288
|
+
|
|
289
|
+
// Remove an appender (flushes and disposes it)
|
|
290
|
+
LogM8.removeAppender('file');
|
|
291
|
+
|
|
292
|
+
// Returns false if the appender doesn't exist
|
|
293
|
+
LogM8.removeAppender('unknown'); // false
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
Note: `addAppender` must be called after `init()` and the appender name must not
|
|
297
|
+
already be in use. To swap an appender's configuration, remove then re-add it.
|
|
298
|
+
|
|
267
299
|
### Adjusting log levels at runtime
|
|
268
300
|
|
|
269
301
|
You can change the effective logging thresholds without recreating loggers:
|
|
@@ -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,NullLogger:()=>C,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}"],x="hh:mm:ss.SSS",F=class{constructor(){i(this,"name",w),i(this,"version",O),i(this,"kind",S),i(this,"_config"),i(this,"_format"),i(this,"_timestampFormat",x),i(this,"_levelMap"),i(this,"_colorEnabled",!1),i(this,"_levelColorMap",{trace:"[37m",track:"[38;5;208m",debug:"[90m",info:"[34m",warn:"[33m",error:"[31m",fatal:"[41m"}),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:x;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+"[0m"}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}},P=class{constructor(){i(this,"name",w),i(this,"version",O),i(this,"kind",S)}create(e){const t=new F;return t.init(e),t}},k="json-formatter",A="1.0.0",j=a.formatter,E=["timestamp","level","logger","message","data"],N=class{constructor(){i(this,"name",k),i(this,"version",A),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",k),i(this,"version",A),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"}],C=class{constructor(){i(this,"isFatal",!1),i(this,"isError",!1),i(this,"isWarn",!1),i(this,"isInfo",!1),i(this,"isDebug",!1),i(this,"isTrack",!1),i(this,"isTrace",!1),i(this,"isEnabled",!1),i(this,"name","nullLogger"),i(this,"level","off"),i(this,"context",{})}fatal(e,...t){}error(e,...t){}warn(e,...t){}info(e,...t){}debug(e,...t){}track(e,...t){}trace(e,...t){}setLevel(e){}setContext(e){this.context=e}getLogger(e){return this}},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 P),this._pluginManager.registerPluginFactory(new B),this._pluginManager.registerPluginFactory(new L)}init(e){var t,r,s,i,n;e=Object.assign({},e),this._reset(),this.setLevel(null!=(t=e.level)?t: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 o=null!=(s=e.appenders)?s:z;for(const e of o){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){const r=(null!=e?e:"").trim().toLowerCase();t?this.getLogger(t).setLevel(r):(this._globalLogLevel=this._logLevelSet.has(r)?r: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){const r=(null!=t?t:"").trim().toLowerCase();e.level=this._logLevelSet.has(r)?r:e.level,e._levelNumber=this._logLevelValues.indexOf(e.level),e.isEnabled=e.level!==l.off;const s=e._levelNumber;e.isFatal=this._logLevelValues.indexOf(l.fatal)<=s,e.isError=this._logLevelValues.indexOf(l.error)<=s,e.isWarn=this._logLevelValues.indexOf(l.warn)<=s,e.isInfo=this._logLevelValues.indexOf(l.info)<=s,e.isDebug=this._logLevelValues.indexOf(l.debug)<=s,e.isTrack=this._logLevelValues.indexOf(l.track)<=s,e.isTrace=this._logLevelValues.indexOf(l.trace)<=s}_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,{ConsoleAppender:()=>u,DefaultFormatter:()=>x,JsonFormatter:()=>N,LogLevel:()=>l,LogM8:()=>D,LogM8Utils:()=>b,MatchFilter:()=>v,NullLogger:()=>B,PACKAGE_INFO:()=>$,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}},d=/(yyyy|SSS|hh|mm|ss|SS|zz|z|yy|MM|dd|A|a|h|S)/g,_=/^\/(.+)\/([dgimsuvy]*)$/,m=new Set(["name","message","stack","cause"]),y=["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(d,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 y)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(_);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=b.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=b.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=b.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",M="1.0.0",A=a.formatter,S=["{timestamp} {LEVEL} [{logger}]","{message}","{data}"],F="hh:mm:ss.SSS",x=class{constructor(){i(this,"name",w),i(this,"version",M),i(this,"kind",A),i(this,"_config"),i(this,"_format"),i(this,"_timestampFormat",F),i(this,"_levelMap"),i(this,"_colorEnabled",!1),i(this,"_levelColorMap",{trace:"[37m",track:"[38;5;208m",debug:"[90m",info:"[34m",warn:"[33m",error:"[31m",fatal:"[41m"}),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:S;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+"[0m"}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}},P=class{constructor(){i(this,"name",w),i(this,"version",M),i(this,"kind",A)}create(e){const t=new x;return t.init(e),t}},O="json-formatter",k="1.0.0",j=a.formatter,E=["timestamp","level","logger","message","data"],N=class{constructor(){i(this,"name",O),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,[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)}}},I=class{constructor(){i(this,"name",O),i(this,"version",k),i(this,"kind",j)}create(e){const t=new N;return t.init(e),t}},C=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"}],$={name:"@ncoderz/log-m8",version:"1.3.0",author:"RA Sewell <richard.sewell@ncoderz.com>",license:"BSD-2-Clause",description:"Logging system for TypeScript / JavaScript"},B=class{constructor(){i(this,"isFatal",!1),i(this,"isError",!1),i(this,"isWarn",!1),i(this,"isInfo",!1),i(this,"isDebug",!1),i(this,"isTrack",!1),i(this,"isTrace",!1),i(this,"isEnabled",!1),i(this,"name","nullLogger"),i(this,"level","off"),i(this,"context",{})}fatal(e,...t){}error(e,...t){}warn(e,...t){}info(e,...t){}debug(e,...t){}track(e,...t){}trace(e,...t){}setLevel(e){}setContext(e){this.context=e}getLogger(e){return this}},D=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,"_logLevelIndexMap"),i(this,"_logBuffer"),this._initialized=!1,this._pluginManager=new C,this._loggers=new Map,this._appenders=[],this._filters=[],this._logLevelValues=Object.values(l),this._logLevelSet=new Set(this._logLevelValues),this._logLevelIndexMap=new Map(this._logLevelValues.map((e,t)=>[e,t])),this._globalLogLevel=l.info,this._globalLogLevelNumber=this._logLevelIndexMap.get(l.info),this._logBuffer=[],this._pluginManager.registerPluginFactory(new p),this._pluginManager.registerPluginFactory(new P),this._pluginManager.registerPluginFactory(new I),this._pluginManager.registerPluginFactory(new L)}init(e){var t,r,s,i;e=Object.assign({},e),this._reset(),this.setLevel(null!=(t=e.level)?t: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 n=null!=(s=e.appenders)?s:z;for(const e of n){const t=this._createAndInitAppender(e);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){const r=(null!=e?e:"").trim().toLowerCase();t?this.getLogger(t).setLevel(r):(this._globalLogLevel=this._logLevelSet.has(r)?r:this._globalLogLevel,this._globalLogLevelNumber=this._logLevelIndexMap.get(this._globalLogLevel))}enableAppender(e){const t=this._getAppender(e);t&&(t.enabled=!0)}disableAppender(e){const t=this._getAppender(e);t&&(t.enabled=!1)}addAppender(e){if(!this._initialized)throw new Error("LogM8: Cannot add appender before init()");const t="string"==typeof e?e:e.name;if(this._getAppender(t))throw new Error(`LogM8: Appender '${t}' already exists`);const r=this._createAndInitAppender(e);this._appenders.push(r),this._sortAppenders()}removeAppender(e){const t=this._getAppender(e);if(!t)return!1;try{t.flush()}catch(e){console&&console.error&&console.error(`LogM8: Failed to flush appender during removal: ${t.name}:`,e)}return t.dispose(),this._appenders=this._appenders.filter(e=>e!==t),!0}getAppenderNames(){return this._appenders.map(e=>e.name)}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){const s=this._logLevelIndexMap.get(t);if(s>e._levelNumber||s>this._globalLogLevelNumber)return;const i=arguments.length>3?Array.prototype.slice.call(arguments,3):[],n={logger:e.name,level:t,message:r,data:i,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){const r=(null!=t?t:"").trim().toLowerCase();e.level=this._logLevelSet.has(r)?r:e.level,e._levelNumber=this._logLevelIndexMap.get(e.level),e.isEnabled=e.level!==l.off;const s=e._levelNumber;e.isFatal=this._logLevelIndexMap.get(l.fatal)<=s,e.isError=this._logLevelIndexMap.get(l.error)<=s,e.isWarn=this._logLevelIndexMap.get(l.warn)<=s,e.isInfo=this._logLevelIndexMap.get(l.info)<=s,e.isDebug=this._logLevelIndexMap.get(l.debug)<=s,e.isTrack=this._logLevelIndexMap.get(l.track)<=s,e.isTrace=this._logLevelIndexMap.get(l.trace)<=s}_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)}}_createAndInitAppender(e){var t;const r=this._pluginManager.createPlugin(a.appender,e),s=b.isString(e)?{name:r.name}:e,i=(null==s?void 0:s.formatter)?this._pluginManager.createPlugin(a.formatter,s.formatter):void 0,n=[],o=s;for(const e of null!=(t=o.filters)?t:[]){const t=this._pluginManager.createPlugin(a.filter,e);t?n.push(t):console&&console.log&&console.log(`LogM8: Filter '${e}' not found for appender ${s.name}.`)}return r.init(s,i,n),r}_getAppender(e){return this._appenders.find(t=>t.name===e)}_sortAppenders(){this._appenders=[...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
|