UNPKG

nestjs-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
import{createLogger as y,transports as x,format as N}from"winston";import{AsyncLocalStorage as l}from"async_hooks";var c=new l;function h(g,e,t,r,...n){let i=new Date().toISOString(),s=c.getStore()?.traceId??"N/A",m=n.find(p=>p&&typeof p=="object")??null,u=r?.name??"N/A",f=typeof r?.time=="number"?`${r.time} ms`:"N/A";return`[${i}] [${g.toUpperCase()}] [${e.toUpperCase()}] [${s}] [${t}] [${m?JSON.stringify(m):"N/A"}] [${u}] [${f}]`}var o=h;var a="nestjs-logitron";var{combine:b,timestamp:L,printf:v,colorize:T}=N,d=class{constructor(e){this.appName=a;this.logger=y({transports:[new x.Console],format:b(T({all:!0}),L({format:"YYYY-MM-DDThh:mm:ss"}),v(t=>`[${t.timestamp}] ${t.level}: ${t.message}`)),...e}),this.appName=e?.appName??a}infoWithExecutionTime(e,t,...r){this.logWithExecutionTime("info",e,t,...r)}warnWithExecutionTime(e,t,...r){this.logWithExecutionTime("warn",e,t,...r)}errorWithExecutionTime(e,t,...r){this.logWithExecutionTime("error",e,t,...r)}debugWithExecutionTime(e,t,...r){this.logWithExecutionTime("debug",e,t,...r)}info(e,...t){let r=o("info",this.appName,e,void 0,...t);this.logger.info(r)}warn(e,...t){let r=o("warn",this.appName,e,void 0,...t);this.logger.warn(r)}error(e,...t){let r=o("error",this.appName,e,void 0,...t);this.logger.error(r)}debug(e,...t){let r=o("debug",this.appName,e,void 0,...t);this.logger.debug(r)}logWithExecutionTime(e,t,r,...n){let i=(performance.now()-r.start).toFixed(2),s=o(e,this.appName,t,{name:r?.name,time:Number(i)},...n);this.logger[e](s)}};export{d as WinstonService};