nodejs-logitron
Version:
Powerful logger module for Nodejs/ Nestjs, seamlessly integrating Pino and Winston for flexible logging with easy configuration.
2 lines (1 loc) • 1.53 kB
JavaScript
import{createLogger as h,transports as E,format as L}from"winston";import{AsyncLocalStorage as u}from"async_hooks";var c=new u;function l(g,t,e,r,...n){let i=new Date().toISOString(),s=c.getStore()?.traceId??"N/A",m=n.find(p=>p&&typeof p=="object")??null,f=r?.name&&typeof r?.time=="number"?`${r.name} ${r.time} ms`:"N/A";return`[${i}] [${g.toUpperCase()}] [${t.toUpperCase()}] [${s}] [${e}] [${m?JSON.stringify(m):"N/A"}] [${f}]`}var o=l;var a="nodejs-logitron";var{combine:T,timestamp:N,printf:y,colorize:I}=L,d=class{constructor(t){this.appName=a;this.logger=h({transports:[new E.Console],format:T(I({all:!0}),N({format:"YYYY-MM-DDThh:mm:ss"}),y(e=>`[${e.timestamp}] ${e.level}: ${e.message}`)),...t}),this.appName=t?.appName??a}infoWithExecutionTime(t,e,...r){this.logWithExecutionTime("info",t,e,...r)}warnWithExecutionTime(t,e,...r){this.logWithExecutionTime("warn",t,e,...r)}errorWithExecutionTime(t,e,...r){this.logWithExecutionTime("error",t,e,...r)}debugWithExecutionTime(t,e,...r){this.logWithExecutionTime("debug",t,e,...r)}info(t,...e){let r=o("info",this.appName,t,void 0,...e);this.logger.info(r)}warn(t,...e){let r=o("warn",this.appName,t,void 0,...e);this.logger.warn(r)}error(t,...e){let r=o("error",this.appName,t,void 0,...e);this.logger.error(r)}debug(t,...e){let r=o("debug",this.appName,t,void 0,...e);this.logger.debug(r)}logWithExecutionTime(t,e,r,...n){let i=(performance.now()-r.start).toFixed(2),s=o(t,this.appName,e,{name:r?.name,time:Number(i)},...n);this.logger[t](s)}};export{d as WinstonService};