UNPKG

@cmtlyt/logger

Version:
2 lines 3.43 kB
let e,t;function o(e){let t=new Map,o=JSON.stringify(e,(e,o)=>{if("bigint"==typeof o)return o.toString();if("function"==typeof o){let e=o.toString();return e.includes("=>")||e.startsWith("function")?e:`function ${e}`}if("object"==typeof o){if(t.has(o)){let e=t.get(o);return`[Circular ${e?`key: ${e}`:"self"}]`}return t.set(o,e),o}return o},2);return o&&"function"==typeof o.replace?o.replace(/\n(\s+)/g,(e,{length:t})=>` ${"\xa0".repeat(t)}`):o}function r(){return"undefined"!=typeof process&&!!(process.versions&&process.versions.node)}let n=(e=!1,t=null,void 0!==globalThis.window?()=>null:async()=>e?t:import("yoctocolors").then(e=>{t=e.default},()=>{e||console.warn("yoctocolors is not available, fallback to plain text, you can customize the shader function through the customColorizer function, or add yoctocolors dependency")}).then(()=>(e=!0,t))),l={error:{type:"red",label:"red",date:"gray",message:"white"},warn:{type:"yellow",label:"yellow",date:"gray",message:"white"},info:{type:"blue",label:"blue",date:"gray",message:"white"},debug:{type:"magenta",label:"magenta",date:"gray",message:"white"},success:{type:"green",label:"green",date:"gray",message:"white"}};function a(e){let{text:t,colorName:o,colors:r,enableColors:n}=e;return n&&r&&r[o]?.(t)||t}function s(e){return e&&"function"==typeof e.replace?e.replace(/\[\s*\]/g,"").replace(/\s+/g," ").trim():e}let i=async e=>{var t;let i="function"==typeof e?.customColorizer?null:await n(),u={options:{enableColors:!1!==(t=e||{}).enableColors,outputLevel:t.outputLevel||"log",allowTypes:t.allowTypes||[],getLabel:t.getLabel||(()=>""),getMessages:t.getMessages||(()=>null),customColors:t.customColors||(()=>({})),customColorizer:t.customColorizer||a,isEnvironmentValid:t.isEnvironmentValid||r,formatDate:t.formatDate||(e=>e.toISOString()),format:t.format||"[%type][%label][%date] %message %othermessages",textCleaner:t.textCleaner||s},colors:i};!i&&u.options.enableColors&&(u.options.enableColors=!1);let c=function(e){let t=new Set(["debug","info","warn","error"]);if(!e)return t;if("function"==typeof e)return e;if(Array.isArray(e)){let o=new Set(t);return e.forEach(e=>{o.add(e)}),o}return e}(u.options.allowTypes);return u.options.isEnvironmentValid()?e=>("function"==typeof c?c(e):c.has(e))?function(e){let{outputLevel:t,getLabel:r,getMessages:n}=e.options;return a=>{let{type:s,messages:i}=a;console[t](function(e,t,r,n){let{options:a}=n,{format:s}=a;return function(e,t,r,n,a){let{options:s,colors:i}=a,{formatDate:u,customColors:c,enableColors:p,customColorizer:f,textCleaner:g}=s,y=l[t]||{type:"cyan",label:"cyan",date:"gray",message:"white"},m=c({type:t})||{},d={...y,...m},b=n.length>0?"string"==typeof n[0]?n[0]:o(n[0]):"",h=n.slice(1).map(e=>"string"==typeof e?e:o(e)).join(" "),w={"%type":f({text:t.toUpperCase(),colorName:d.type,colors:i,enableColors:p,part:"type"}),"%label":r?f({text:r,colorName:d.label,colors:i,enableColors:p,part:"label"}):"","%date":f({text:u(new Date),colorName:d.date,colors:i,enableColors:p,part:"date"}),"%message":f({text:b,colorName:d.message,colors:i,enableColors:p,part:"message"}),"%othermessages":h?f({text:h,colorName:d.message,colors:i,enableColors:p,part:"othermessages"}):""},C=Object.keys(w),v=e;for(let e=0;e<C.length;e++){let t=C[e],o=w[t];v=v.replaceAll(t,o)}return g(v)}(s,e,t,r,n)}(s,r(a)||"",(n(a)||i).filter(e=>"object"!=typeof e||null===e||!("module"in e)),e))}}(u):null:{}};export{i as nodeConsoleAdapter};