From a451b3008e17167198da38ec627bb28d8af084de Mon Sep 17 00:00:00 2001 From: Marc Pervaz Boocha Date: Fri, 27 Feb 2026 21:18:28 +0530 Subject: Add Syslog and refactored the Sink api --- logging/test/handler.go | 10 ++++++---- logging/test/handler_test.go | 36 ++++-------------------------------- 2 files changed, 10 insertions(+), 36 deletions(-) (limited to 'logging/test') diff --git a/logging/test/handler.go b/logging/test/handler.go index e26916c..210893b 100644 --- a/logging/test/handler.go +++ b/logging/test/handler.go @@ -14,7 +14,7 @@ import ( type logRecorder struct { mu sync.Mutex - records []slog.Record + records []map[string]any } var _ sinks.Sink = &logRecorder{} @@ -23,7 +23,9 @@ func (h *logRecorder) Append(r slog.Record) error { h.mu.Lock() defer h.mu.Unlock() - h.records = append(h.records, r.Clone()) + it := sinks.RecordAll(r, nil) + + h.records = append(h.records, sinks.ToMap(it)) return nil } @@ -31,7 +33,7 @@ func (h *logRecorder) Enabled(level slog.Level) bool { return true // Capture all logs } -func (h *logRecorder) Records() []slog.Record { +func (h *logRecorder) Records() []map[string]any { h.mu.Lock() defer h.mu.Unlock() return slices.Clone(h.records) @@ -65,6 +67,6 @@ func NewMockLogHandler(tb testing.TB) *MockHandler { // Records returns a copy of all slog.Records captured by this handler so far. // // It is safe to call from multiple goroutines. -func (h *MockHandler) Records() []slog.Record { +func (h *MockHandler) Records() []map[string]any { return h.recorder.Records() } diff --git a/logging/test/handler_test.go b/logging/test/handler_test.go index ff1ad01..e78192e 100644 --- a/logging/test/handler_test.go +++ b/logging/test/handler_test.go @@ -4,11 +4,9 @@ import ( "fmt" "log/slog" "runtime" - "strings" "testing" "testing/slogtest" - "go.sudomsg.com/kit/logging/sinks" "go.sudomsg.com/kit/logging/test" ) @@ -28,16 +26,11 @@ func TestMockHandlerCompliance(t *testing.T) { t.Fatalf("no records captured") } - rec := recs[len(recs)-1] + m := recs[len(recs)-1] - // Convert slog.Record to map[string]any - m := make(map[string]any) - for key, value := range sinks.RecordAll(rec) { - insertIntoMap(m, key, value) - } - - if _, ok := m[slog.SourceKey]; ok { - fs := runtime.CallersFrames([]uintptr{rec.PC}) + if v, ok := m[slog.SourceKey]; ok { + pc := uintptr(v.(uint64)) + fs := runtime.CallersFrames([]uintptr{pc}) f, _ := fs.Next() m[slog.SourceKey] = fmt.Sprintf("%s:%d", f.File, f.Line) } @@ -46,24 +39,3 @@ func TestMockHandlerCompliance(t *testing.T) { }, ) } - -func insertIntoMap(m map[string]any, dottedKey string, value slog.Value) { - parts := strings.Split(dottedKey, ".") - key := parts[len(parts)-1] - parts = parts[:len(parts)-1] - currentMap := m - - for _, part := range parts { - if _, ok := currentMap[part]; !ok { - currentMap[part] = make(map[string]any) - } - - if nested, ok := currentMap[part].(map[string]any); ok { - currentMap = nested - } else { - currentMap[part] = make(map[string]any) - currentMap = currentMap[part].(map[string]any) - } - } - currentMap[key] = value.Any() -} -- cgit v1.3.1