import*as O from"node:path";import{isProductionMode as R}from"../../utils/envs/is-production-mode.js";import{maskEmail as T}from"./helpers/privacy/mask-email.js";import{maskSubject as E}from"./helpers/privacy/mask-subject.js";import{removePii as $}from"./helpers/privacy/remove-pii.js";import{removePiiFromUrl as A}from"./helpers/privacy/remove-pii-from-url.js";import{red as P,green as x,bold as w,blue as p,gray as n,yellow as b,cyan as F}from"./helpers/colors.js";var s;(function(e){e.INFO="INFO",e.WARN="WARN",e.ERROR="ERROR",e.SUCCESS="SUCCESS",e.VERBOSE="VERBOSE",e.HTTP="HTTP"})(s||(s={}));const N={[s.INFO]:p,[s.SUCCESS]:x,[s.WARN]:b,[s.ERROR]:P,[s.HTTP]:F,[s.VERBOSE]:n},C={"%ap":e=>p(O.resolve(e)),"%rp":e=>p(O.relative(process.cwd(),e)),"%s":e=>e?.toString(),"%c":e=>p(e)},v={format(e){switch(process.env.PORTAL_LOG_FORMAT){case"JSON":return H(e);default:return I(e)}},interpolate(e,...c){const i=Object.keys(C).map(t=>`(${t})`).join("|"),a=new RegExp(i,"g");let o,m=e;for(;(o=a.exec(e))!==null;){o.index===a.lastIndex&&a.lastIndex++;const t=c.shift();if(t===void 0)break;const r=o[0];m=m.replace(r,C[r](t))}return m}};var W=v;const I=e=>{const{level:c,message:i,duration:a,context:o}=e,m=R(),t=[N[c](w(`[${c.toLowerCase()}]`))];if(m&&t.push(n(`time="${j()}"`)),o){const{email:r,ipAddress:u,subject:l,method:d,pathname:f,statusCode:h,teams:S,apiFunction:g}=o;g&&t.push(p(`fn="${g}"`)),h&&t.push(y(h)),e.level===s.HTTP&&(d&&t.push(n(`method="${d}"`)),f&&t.push(n(`path="${A(f)}"`))),u&&t.push(n(`ip="${u}"`)),r&&t.push(n(`email="${T(r)}"`)),l&&t.push(n(`sub="${E(l)}"`)),S?.length&&t.push(n(`teams="${S.join(", ")}"`))}if(i){const r=m?`msg="${String($(i)).replaceAll?.('"','\\"')}"`:String(i);t.push(r)}return e.level===s.HTTP&&o?.userAgent&&t.push(n(`agent="${o.userAgent}"`)),a&&t.push(M(a)),t.join(" ")+`
`},H=({context:e,message:c,...i})=>{const{method:a,pathname:o,statusCode:m,userAgent:t,subject:r,email:u,teams:l,apiFunction:d}=e||{},f={...i,email:u&&T(u),subject:r&&E(r),teams:l,apiFunction:d,...i.level===s.HTTP?{method:a,pathname:String(A(o)),statusCode:m,userAgent:t}:{message:String($(c))}};return R()&&(f.time=j()),JSON.stringify(f,["time","level","scope","message","duration","method","pathname","statusCode","userAgent","subject","ipAddress","email","apiFunction"])+`
`},y=e=>(e%500<100?P:e%400<100?b:x)(`status="${e}"`),j=()=>{let e=new Date().getTimezoneOffset()*6e4;return new Date(Date.now()-e).toISOString().slice(0,-1)},M=e=>n(`dur="${Math.round(e)}ms"`);export{s as LogLevel,W as default};
