import{Logger as m}from"../../tools/notifiers/logger.js";import{getClientIp as u}from"../utils/get-client-ip.js";import{LogLevel as d}from"../../tools/notifiers/formatter.js";function c(){return async(e,r)=>{const t=new m({context:g(e)});if(e.set("logger",t),!t.shouldLog(d.HTTP))return await r();const s=t.startTiming();try{await r(),t.updateContext(a(e)),t.httpTime(s)}catch(o){throw t.updateContext(a(e)),t.httpTime(s),o}}}const g=e=>{const r=e.req.method,t=new URL(e.req.url).pathname,s=e.req.header("user-agent")||void 0,o=u(e.req.raw),i=e.req.raw.context.remoteAddr,n=e.get("auth");return{email:n?.claims?.email,subject:n?.claims?.sub,teams:n?.teams,ipAddress:o||i?.hostname,userAgent:s,method:r,pathname:t}},a=e=>({statusCode:e.res.status});export{c as loggerMiddleware};
