package middleware import ( "time" "github.com/labstack/echo/v4" "github.com/rs/zerolog/log" "github.com/treytartt/honeydue-api/internal/models" ) // StructuredLogger is zerolog-based request logging middleware that includes // correlation IDs, user IDs, and latency metrics. func StructuredLogger() echo.MiddlewareFunc { return func(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { start := time.Now() err := next(c) latency := time.Since(start) // Build structured log event event := log.Info() if c.Response().Status >= 500 { event = log.Error() } else if c.Response().Status >= 400 { event = log.Warn() } // Request ID if reqID := GetRequestID(c); reqID != "" { event = event.Str("request_id", reqID) } // User ID (from auth middleware) if user, ok := c.Get(AuthUserKey).(*models.User); ok && user != nil { event = event.Uint("user_id", user.ID) } event. Str("method", c.Request().Method). Str("path", c.Path()). Str("uri", c.Request().RequestURI). Int("status", c.Response().Status). Int64("latency_ms", latency.Milliseconds()). Str("remote_ip", c.RealIP()). Msg("request") return err } } }