logging-pretty
Version:
Logging your message with pretty color with node.js
3 lines (2 loc) • 1.42 kB
JavaScript
import f from"chalk";import T from"dayjs";import{appendFile as m}from"fs/promises";var{red:r,green:l,yellow:c,cyan:p,blue:h,bgRed:u,bold:d,white:y}=f,g=class{pathFile;force;formatTime;isHavePathFile;constructor(i={}){let{pathFile:t,force:e,formatTime:s}=i;this.pathFile=t,this.isHavePathFile=!!t,e?this.force=e:this.force=this.isHavePathFile?"all":"console",this.formatTime=s||"YYYY-MM-DD HH:mm:ss:SSS"}_getTimeNow(i){return T().format(i)}_renderLogToConsole(i){let{strTime:t,strStyleTag:e,strStyleMsg:s}=i;console.log(`[${t}] [${e}] ${s}`)}_writeLogToFile(i){let{strTime:t,strTag:e,strMsg:s}=i,o=`[${t}] [${e}] ${s}
`;m(this.pathFile,o)}_log(i,t,e,s){let o=this._getTimeNow(this.formatTime),n=e(i);if(this.force==="console"||this.force==="all"){let a=t;s&&(a=s(t)),this._renderLogToConsole({strTime:o,strTag:i,strStyleTag:n,strMsg:t,strStyleMsg:a})}this.isHavePathFile&&(this.force==="file"||this.force==="all")&&this._writeLogToFile({strTime:o,strTag:i,strStyleTag:n,strMsg:t,strStyleMsg:t})}info(i){this._log("INFO",i,l,void 0)}warn(i){this._log("WARN",i,c,c)}error(i){this._log("ERROR",i,r,r)}success(i){this._log("SUCCESS",i,l,l)}failed(i){this._log("FAILED",i,r,r)}fail(i){this._log("FAIL",i,r,r)}debug(i){this._log("DEBUG",i,p,p)}trace(i){this._log("TRACE",i,h,h)}fatal(i){this._log("FATAL",i,r,u)}custom(i,t,e=d,s=y){this._log(i,t,e,s)}get _listColor(){return f}};var S=g;export{g as LoggingPretty,S as default};