UNPKG

logging-pretty

Version:

Logging your message with pretty color with node.js

3 lines (2 loc) 2.07 kB
var b=Object.create;var g=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var _=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,A=Object.prototype.hasOwnProperty;var L=(s,i)=>{for(var t in i)g(s,t,{get:i[t],enumerable:!0})},f=(s,i,t,r)=>{if(i&&typeof i=="object"||typeof i=="function")for(let e of _(i))!A.call(s,e)&&e!==t&&g(s,e,{get:()=>i[e],enumerable:!(r=F(i,e))||r.enumerable});return s};var T=(s,i,t)=>(t=s!=null?b(S(s)):{},f(i||!s||!s.__esModule?g(t,"default",{value:s,enumerable:!0}):t,s)),w=s=>f(g({},"__esModule",{value:!0}),s);var E={};L(E,{LoggingPretty:()=>n,default:()=>$});module.exports=w(E);var c=T(require("chalk")),y=T(require("dayjs")),v=require("fs/promises"),{red:o,green:a,yellow:m,cyan:u,blue:d,bgRed:C,bold:H,white:R}=c.default,n=class{pathFile;force;formatTime;isHavePathFile;constructor(i={}){let{pathFile:t,force:r,formatTime:e}=i;this.pathFile=t,this.isHavePathFile=!!t,r?this.force=r:this.force=this.isHavePathFile?"all":"console",this.formatTime=e||"YYYY-MM-DD HH:mm:ss:SSS"}_getTimeNow(i){return(0,y.default)().format(i)}_renderLogToConsole(i){let{strTime:t,strStyleTag:r,strStyleMsg:e}=i;console.log(`[${t}] [${r}] ${e}`)}_writeLogToFile(i){let{strTime:t,strTag:r,strMsg:e}=i,l=`[${t}] [${r}] ${e} `;(0,v.appendFile)(this.pathFile,l)}_log(i,t,r,e){let l=this._getTimeNow(this.formatTime),p=r(i);if(this.force==="console"||this.force==="all"){let h=t;e&&(h=e(t)),this._renderLogToConsole({strTime:l,strTag:i,strStyleTag:p,strMsg:t,strStyleMsg:h})}this.isHavePathFile&&(this.force==="file"||this.force==="all")&&this._writeLogToFile({strTime:l,strTag:i,strStyleTag:p,strMsg:t,strStyleMsg:t})}info(i){this._log("INFO",i,a,void 0)}warn(i){this._log("WARN",i,m,m)}error(i){this._log("ERROR",i,o,o)}success(i){this._log("SUCCESS",i,a,a)}failed(i){this._log("FAILED",i,o,o)}fail(i){this._log("FAIL",i,o,o)}debug(i){this._log("DEBUG",i,u,u)}trace(i){this._log("TRACE",i,d,d)}fatal(i){this._log("FATAL",i,o,C)}custom(i,t,r=H,e=R){this._log(i,t,r,e)}get _listColor(){return c.default}};var $=n;0&&(module.exports={LoggingPretty});