[PHASE-6] Add logger helper
This commit is contained in:
parent
cf7e6a6f2b
commit
9d10f385b5
27
internal/lib/sl/sl.go
Normal file
27
internal/lib/sl/sl.go
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
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{})
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue