UNPKG

@waiting/log

Version:

Lightweight logging for browser

13 lines (11 loc) 3.64 kB
/** * @waiting/log * Lightweight logging for browser * * @version 1.0.2 * @author waiting * @license MIT * @link https://github.com/waitingsong/log#readme */ const e={maxMsgLength:1024,runLevel:"log",persist:!0,persistTTL:604800,storagePrefix:"log"};var n;function t(e,n){const t=e.split("-");if(t.length<3)return!1;const r=t.shift(),o=t.shift(),i=t.join("-");if(r!==n)return!1;if(!o)return!1;if(!i)return!1;{const e=new Date(i);if("function"!=typeof e.getTime||Number.isNaN(e.getTime()))return!1}return!0}function r(e,t){return n[t]<n[e]}function o(e,n){return!!e&&(!n||e===n)}function i(e,n){if(!e)return!1;const t=new Date(e);return t<n}function f(n){u(n),e.maxMsgLength=Math.ceil(n)}function u(e){if("number"!=typeof e||e<=0)throw new TypeError("Value of parameter maxMsgLenth invalid")}function a(n){s(n),e.persist=n}function s(e){if("boolean"!=typeof e)throw new TypeError("Value of parameter persist invalid")}function c(n){l(n),e.persistTTL=Math.ceil(n)}function l(e){if("number"!=typeof e)throw new TypeError("Value of parameter ttl invalid");if(+e<0)throw new TypeError("Value of parameter ttl invalid")}function g(n){p(n),e.runLevel=n}function p(e){if("silent"!==e&&"function"!=typeof console[e])throw new TypeError(`Function console.${e}() NOT exists`);if(!e||"number"!=typeof n[e])throw new TypeError("Value of parameter logLevel invalid: "+e)}function y(n){h(n),e.storagePrefix=n.replace(/-/g,"_")}function h(e){if(!e||"string"!=typeof e)throw new TypeError("Value of parameter key invalid")}function m(e,n,t,r){if("silent"===e)return"";const o=(new Date).toISOString(),i=`${t}-${e}-${o}`;return w(i,n,r),i}function w(e,n,t){let r=n;"string"==typeof n&&n.length>t&&(r=n.slice(0,t)+" ...");const o=JSON.stringify(r);if("object"==typeof localStorage)try{localStorage.setItem(encodeURIComponent(e),o)}catch(e){console.error(e)}}function b(e,n){if("silent"!==e)if(Array.isArray(n)){console.group("Group log:");for(let t=0;t<n.length;t++)console[e](n[t]);console.groupEnd()}else console[e](n)}function v(e,n){return j("trace",e,n)}function T(e,n){return j("debug",e,n)}function d(e,n){return j("log",e,n)}function L(e,n){return j("info",e,n)}function x(e,n){return j("warn",e,n)}function E(e,n){return j("error",e,n)}function k(e,n){return j("silent",e,n)}function N(){return Object.assign({},e)}function M(e){if(e&&"object"==typeof e)for(const[n,t]of Object.entries(e))switch(n){case"maxMsgLength":f(t);break;case"persist":a(t);break;case"persistTTL":c(t);break;case"runLevel":g(t);break;case"storagePrefix":y(t);break;default:L(`setConfig(config): value of param invalid: key "${n}"/value "${t}"`)}else L(`setConfig(config): value of param invalid: "${e}"`);return N()}function $(){const e=[];return Object.keys(n).forEach(n=>{if(n&&"string"==typeof n&&Number.isNaN(+n))try{p(n),e.push(n)}catch(e){}}),e}function j(n,t,o){if(r(e.runLevel,n))return;if("silent"===n)return;b(n,t);const i="boolean"==typeof o?o:e.persist;i&&m(n,t,e.storagePrefix,e.maxMsgLength)}!function(e){e[e.trace=0]="trace",e[e.debug=1]="debug",e[e.log=2]="log",e[e.info=3]="info",e[e.warn=4]="warn",e[e.error=5]="error",e[e.silent=6]="silent"}(n||(n={}));export{v as trace,T as debug,d as log,L as info,x as warn,E as error,k as silent,N as getConfig,M as setConfig,$ as getAvailableLogLevel,n as LogLevel,t as isLocalStorageKey,r as isSuppressLogLevel,o as isLogLevelToPrune,i as isTimeToPrune,f as setMaxMsgLength,u as validateMaxMsgLength,a as setPersist,s as validatePersist,c as setPersistTTL,l as validatePersistTTL,g as setRunLevel,p as validateLogLevel,y as setStoragePrefix,h as validateStoragePrefix}; //# sourceMappingURL=log.esm.min.js.map