logpipes
Version:
Console.log transformation pipes
2 lines • 8.86 kB
JavaScript
const e=["debug","error","info","log","trace","warn"],t=[],r=()=>{},n={debug:r,error:r,info:r,log:r,trace:r,warn:r};function o(e){i(...Array.isArray(e)?e:[e])}function i(...o){!function(){if(n.debug===r)for(const r of e)n[r]=console[r],console[r]=(...e)=>{var o,i,s;let a=r,c=e;for(const e of t){const t=e(a,...c);if(!t||(Array.isArray(t)?0===(null!==(o=null==t?void 0:t.length)&&void 0!==o?o:0):0===(null!==(s=null===(i=t.args)||void 0===i?void 0:i.length)&&void 0!==s?s:0)))return;Array.isArray(t)?c=t:(a=t.level,c=t.args)}n[a](...c)}}();for(const e of o)!t.includes(e)&&e.onInstall&&e.onInstall(),t.push(e)}function s(e){a(...Array.isArray(e)?e:[e])}function a(...o){for(const e of o)for(let r=t.indexOf(e);r>=0;r=t.indexOf(e)){const e=t.splice(r,1)[0];!t.includes(e)&&e.onUninstall&&e.onUninstall()}!function(){if(!(t.length>0)&&n.debug!==r)for(const t of e)console[t]=n[t],n[t]=r}()}function c(){for(const e of[...t])s(e)}function l(){return[...t]}function u(){if(n.debug!==r)return Object.assign({},n);const t={};for(const r of e)t[r]=console[r];return t}function f(){return{dateFormatter:e=>new Date(e).toISOString()}}function g({dateFormatter:e}=f()){return(t,...r)=>[e(Date.now()),...r]}function y(){return{maxDepthLimit:10,maxArrayLength:100,maxObjectPropertyCount:100,isIgnoredProperty:()=>!1,replacePropertyValue:(e,t)=>t,depthLimitValue:"[Depth limit ~]",arrayLengthLimitValue:"[Array, length: $length ~]",objectPropertyCountLimitValue:"[Object, properties: $count ~]",circularReferenceValue:"[Circular ~]",functionValue:"[Function ~]",symbolValue:"[Symbol ~]"}}const p={maxDepthLimit:10,maxArrayLength:100,maxObjectPropertyCount:100,isIgnoredProperty:()=>!1,replacePropertyValue:(e,t)=>t,depthLimitValue:"[Depth limit ~]",arrayLengthLimitValue:"[Array, length: $length ~]",objectPropertyCountLimitValue:"[Object, properties: $count ~]",circularReferenceValue:"[Circular ~]",functionValue:"[Function ~]",symbolValue:"[Symbol ~]"},m=["cause","message","name","stack"];function d(e,t={},r=0,n=new Set){const o=Object.assign(Object.assign({},p),t);if(r>o.maxDepthLimit)return o.depthLimitValue;if("string"==typeof(e=b(e))||"boolean"==typeof e||"number"==typeof e||null==e)return e;if(n.has(e))return o.circularReferenceValue;if(n.add(e),Array.isArray(e))return e.length>o.maxArrayLength?o.arrayLengthLimitValue.replace("$length",`${e.length}`):e.map((e=>d(e,o,r+1,n)));const i=Object.entries(e);if(i.length>o.maxObjectPropertyCount)return o.objectPropertyCountLimitValue.replace("$count",`${i.length}`);const s={};for(const[e,t]of i)0===r&&o.isIgnoredProperty(e)||(s[e]=d(t,o,r+1,n));for(const t of m)if(!s[t]&&!o.isIgnoredProperty(t)){const i=e[t];void 0!==i&&(s[t]=d(i,o,r+1,n))}if(o.replacePropertyValue!==p.replacePropertyValue)for(const[e,t]of Object.entries(s))s[e]=o.replacePropertyValue(e,t);return s}function b(e,t={}){if(null==e)return e;switch(typeof e){case"undefined":case"boolean":case"string":return e;case"bigint":return`BigInt(${e.toString()})`;case"number":return isNaN(e)?"NaN":e===1/0?"Infinity":e===-1/0?"-Infinity":e;case"function":return t.functionValue||p.functionValue;case"symbol":return t.symbolValue||p.symbolValue;case"object":if(e instanceof Set)return[...e.keys()];if(e instanceof Map)return Object.fromEntries([...e.entries()]);if(e instanceof String||e instanceof Number||e instanceof Boolean)return e.valueOf();if(e instanceof Date)return e.toISOString()}return e}function h(){return Object.assign(Object.assign({},{maxDepthLimit:10,maxArrayLength:100,maxObjectPropertyCount:100,isIgnoredProperty:()=>!1,replacePropertyValue:(e,t)=>t,depthLimitValue:"[Depth limit ~]",arrayLengthLimitValue:"[Array, length: $length ~]",objectPropertyCountLimitValue:"[Object, properties: $count ~]",circularReferenceValue:"[Circular ~]",functionValue:"[Function ~]",symbolValue:"[Symbol ~]"}),{messagePropertyName:"message",levelPropertyName:"level",levelPropertyFormatter:e=>e,timestampPropertyName:"timestamp",timestampPropertyFormatter:e=>new Date(e).toISOString(),messageIdPropertyName:"message_id",messageIdPropertyProvider:v,isIgnoredProperty:()=>!1,getObjectMessageToken:e=>`$${e+1}`,pickFieldNameAsObjectMessageTokenForSingleFieldObjects:!1,undefinedMessageValue:void 0})}function x(e={}){const t=h(),r=Object.assign(Object.assign({},t),e);let n,o="";const i=(e,...i)=>{const s={};let a;s[r.messagePropertyName]=void 0;let c=0;for(let e=0;e<i.length;e++){const t=b(i[e]);let n=t;if("object"==typeof t&&null!==t){let o=d(t,r);if(r.pickFieldNameAsObjectMessageTokenForSingleFieldObjects&&"object"==typeof o&&null!==o){const e=Object.entries(o);if(1===e.length){const[t,r]=e[0],i=`$${t}`;if(void 0===s[i])if(n=i,null===(l=r)||"string"==typeof l||void 0===l||"number"==typeof l||"boolean"==typeof l){const e="string"==typeof r?"'":"";n+=`:[${e}${r}${e}]`,o=void 0}else o=r}}"string"!=typeof n&&(n=r.getObjectMessageToken(c,t,e),c++),s[n]=o}else void 0===t?void 0!==r.undefinedMessageValue&&(n+=r.undefinedMessageValue):n=t;a=void 0===a?`${n}`:`${a} ${n}`}var l;if(a&&(s[r.messagePropertyName]=a),r.levelPropertyName&&(s[r.levelPropertyName]=r.levelPropertyFormatter(e)),r.timestampPropertyName&&(s[r.timestampPropertyName]=r.timestampPropertyFormatter(Date.now())),r.messageIdPropertyName){let a=n||r.messageIdPropertyProvider(e,...i);void 0===a&&(a=t.messageIdPropertyProvider()),o=a,s[r.messageIdPropertyName]=o,n=void 0}return[s]};return i.getLastMessageId=()=>o,i.setNextMessageId=e=>{n=e},i}function v(){let e=Date.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,(t=>{const r=(e+Math.floor(16*Math.random()))%16;return e=Math.floor(e/16),("x"===t?r:3&r|8).toString(16)}))}function S(){return Object.assign(Object.assign({},h()),{preStringifyCallback:()=>{}})}function P(e={}){const t=Object.assign(Object.assign({},S()),e),r=x(t),n=(e,...n)=>{const o=r(e,...n);if(0===o.length)return[];const i=o[0];return t.preStringifyCallback(i),[JSON.stringify(i)]};return n.getLastMessageId=r.getLastMessageId,n.setNextMessageId=r.setNextMessageId,n}function j(){return{cacheSize:1e3,cacheSizeByStringify:-1}}function O(e={}){const t=Object.assign(Object.assign({},{cacheSize:1e3,cacheSizeByStringify:-1}),e);if(t.cacheSize<0||isNaN(t.cacheSize))throw new Error(`Invalid cache size: ${t.cacheSize}`);const r={size:0};let n=!1,o=0;function i(){r.first&&(o>0&&(o-=L(...r.first.value.args)),r.first=r.first.next,r.size--)}const s=(e,...a)=>{if(0===t.cacheSize||n)return a;var c;c={value:{level:e,args:a,timestamp:Date.now()}},void 0===r.last?r.first=c:r.last.next=c,r.last=c,r.size++,t.cacheSizeByStringify>=0&&(o+=L(...c.value.args));const l=r.size>t.cacheSize,u=t.cacheSizeByStringify>=0&&o>t.cacheSizeByStringify;if((l||u)&&t.onCacheSizeReached){n=!0;try{t.onCacheSizeReached(s)}finally{n=!1}}if(u){for(;o>t.cacheSizeByStringify&&void 0!==r.first;)i();o=Math.max(o,0),void 0===r.first&&(o=0)}else l&&i();return a};return s.getMessages=()=>{const e=[];let t=r.first;for(;void 0!==t;)e.push(t.value),t=t.next;return e},s.clearMessages=()=>{r.first=void 0,r.last=void 0,r.size=0,o=0},s.onInstall=()=>s.clearMessages(),s}function L(...e){let t=0;for(const r of e)void 0!==r&&(t+=JSON.stringify(d(r)).length);return t}function V(e={}){const t=Object.assign({excludedLogLevels:[]},e);return(e,...r)=>{const n="function"==typeof t.excludedLogLevels?t.excludedLogLevels(e):t.excludedLogLevels;return("boolean"==typeof n?n:n.includes(e))?[]:r}}function I(){return{isCaseSensitive:!1,excludedMessageTokens:[]}}function N(e){const{excludedMessageTokens:t,isCaseSensitive:r}=Object.assign(Object.assign({},{isCaseSensitive:!1,excludedMessageTokens:[]}),e),n=t.filter((e=>"string"==typeof e)).map((e=>r?e:e.toLowerCase())),o=t.filter((e=>"object"==typeof e));return t.sort(((e,t)=>typeof e==typeof t?0:"string"==typeof e?-1:1)),(e,...i)=>{if(0===t.length)return i;if(r){for(const e of i)if("string"==typeof e){if(n.some((t=>e.includes(t))))return[];if(o.some((t=>t.test(e))))return[]}}else for(const e of i)if("string"==typeof e){const t=e.toLowerCase();if(n.some((e=>t.includes(e))))return[];if(o.some((t=>t.test(e))))return[]}return i}}function M(){return(e,...t)=>t}export{e as LOG_LEVELS,g as createDateTimePipe,x as createJsonPipe,P as createJsonStringifyPipe,O as createLogCachePipe,V as createLogLevelFilterPipe,N as createLogMessageFilterPipe,M as createNoopPipe,L as estimateArgsSizeByStringify,v as generateUuidSimple,l as getConsoleOverrides,f as getDefaultDateTimePipeOptions,h as getDefaultJsonPipeOptions,y as getDefaultJsonSimplifierOptions,S as getDefaultJsonStringifyPipeOptions,j as getDefaultLogCachePipeOptions,I as getLogMessageFilterPipeOptions,u as getOriginalConsoleMethods,o as installConsoleOverride,i as installConsoleOverrides,d as simplifyJson,b as simplifyValue,c as uninstallAllConsoleOverrides,s as uninstallConsoleOverride,a as uninstallConsoleOverrides};
//# sourceMappingURL=index.esm.js.map