hash-of-wisdom/internal/lib/sl/sl.go

28 lines
769 B
Go

package sl
import (
"context"
"log/slog"
)
// Err creates a structured error attribute for slog
func Err(err error) slog.Attr {
return slog.Attr{
Key: "error",
Value: slog.StringValue(err.Error()),
}
}
// MockHandler is a test handler that discards all log messages
type MockHandler struct{}
func (h *MockHandler) Enabled(context.Context, slog.Level) bool { return false }
func (h *MockHandler) Handle(context.Context, slog.Record) error { return nil }
func (h *MockHandler) WithAttrs([]slog.Attr) slog.Handler { return h }
func (h *MockHandler) WithGroup(string) slog.Handler { return h }
// NewMockLogger creates a logger that discards all messages for testing
func NewMockLogger() *slog.Logger {
return slog.New(&MockHandler{})
}