package reqlog import ( "context" "log/slog" "runtime/debug" "github.com/food-ai/backend/internal/infra/middleware" ) // Handler is a slog.Handler wrapper that enriches ERROR-level records with // the request_id from context and a goroutine stack trace. type Handler struct { inner slog.Handler } // New wraps inner with request-aware enrichment. func New(inner slog.Handler) *Handler { return &Handler{inner: inner} } func (handler *Handler) Handle(ctx context.Context, record slog.Record) error { if record.Level >= slog.LevelError { if requestID := middleware.RequestIDFromCtx(ctx); requestID != "" { record.AddAttrs(slog.String("request_id", requestID)) } record.AddAttrs(slog.String("stack", string(debug.Stack()))) } return handler.inner.Handle(ctx, record) } func (handler *Handler) Enabled(ctx context.Context, level slog.Level) bool { return handler.inner.Enabled(ctx, level) } func (handler *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { return &Handler{inner: handler.inner.WithAttrs(attrs)} } func (handler *Handler) WithGroup(name string) slog.Handler { return &Handler{inner: handler.inner.WithGroup(name)} }