hagen
Version:
A colorful logger for JS in Node and the Browser
2 lines (1 loc) • 2.46 kB
JavaScript
var C=Object.defineProperty;var d=Object.getOwnPropertySymbols;var p=Object.prototype.hasOwnProperty,k=Object.prototype.propertyIsEnumerable;var h=(e,o,n)=>o in e?C(e,o,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[o]=n,c=(e,o)=>{for(var n in o||(o={}))p.call(o,n)&&h(e,n,o[n]);if(d)for(var n of d(o))k.call(o,n)&&h(e,n,o[n]);return e};import{Chalk as v}from"chalk";import{isCI as m}from"std-env";var l=new v({level:3});m&&(l.level=0);var u={showTimestamp:!1,colors:{reserved:{WARN:l.bgYellowBright.black,ERROR:l.bgRedBright.black,INFO:l.bgBlack.white,SUCCESS:l.bgBlack.greenBright},normal:[l.bgBlue.white,l.bgGreen.black,l.bgCyan.black,l.bgRed.white,l.bgMagenta.white,l.bgYellow.black]}},t=c({},u);function x(e){t=c(c({},t),e)}function B(){return t}function N(){t=c({},u)}function w(e){let o=Array.from(e).reduce((n,i)=>n+i.charCodeAt(0),0);return t.colors.normal[o%t.colors.normal.length]}function $(e,o,n="end"){if(e.length===o)return e;if(e.length<o){let a=o-e.length,s=Math.floor(a/2),b=a-s;return" ".repeat(s)+e+" ".repeat(b)}let i="\u2026",r=o-1;switch(n){case"start":return`${i}${e.slice(e.length-r)}`;case"end":return`${e.slice(0,r)}${i}`;case"middle":{let a=Math.ceil(r/2),s=Math.floor(r/2);return`${e.slice(0,a)}${i}${e.slice(e.length-s)}`}default:throw new Error(`Invalid truncation method: ${n}`)}}function g({logger:e,label:o,data:n}){let i,r="\u2022";if(typeof o=="object"?(r=o.label?o.label:r,"color"in o?typeof o.color=="number"?i=t.colors.normal[o.color]:i=o.color:i=l.bgHex(o.bgColor).hex(o.fgColor)):(r=o||r,i=w(o)),r=r.trim(),t.fixedWidth!==void 0&&(r=$(r,t.fixedWidth.width,t.fixedWidth.truncationMethod)),r=i.bold(` ${r} `),m&&(r=`[${r}]`),t.showTimestamp){let a=new Date().toISOString(),s=l.gray(`[ ${a} ]`);r=`${r} ${s}`}e(r,...n)}function f(e,o){return`${e} ${o}`.trim()}function I(e,...o){g({logger:console.log,label:e,data:o})}function R(e,...o){g({logger:console.log,label:{label:f("i",e),color:t.colors.reserved.INFO},data:o})}function y(e,...o){g({logger:console.log,label:{label:f("\u2713",e),color:t.colors.reserved.SUCCESS},data:o})}function S(e,...o){g({logger:console.warn,label:{label:f("!",e),color:t.colors.reserved.WARN},data:o})}function L(e,...o){g({logger:console.error,label:{label:f("\u2715",e),color:t.colors.reserved.ERROR},data:o})}var A={log:I,info:R,success:y,warn:S,error:L};export{A as default,L as error,B as getConfig,R as info,I as log,N as resetConfig,x as setConfig,y as success,S as warn};