f5061f53db
Remove race conditions from -race in tests * timeout * raceconditions * nobuildgp * build * perf
41 lines
664 B
Go
41 lines
664 B
Go
package logger
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
type TestLogger struct {
|
|
T *testing.T
|
|
Logs []map[string]any
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func (l *TestLogger) Handle(ctx context.Context, r slog.Record) error {
|
|
attrs := make(map[string]any)
|
|
r.Attrs(func(a slog.Attr) bool {
|
|
attrs[a.Key] = a.Value.Any()
|
|
return true
|
|
})
|
|
|
|
l.mu.Lock()
|
|
defer l.mu.Unlock()
|
|
l.Logs = append(l.Logs, attrs)
|
|
|
|
return nil
|
|
}
|
|
|
|
func (l *TestLogger) Enabled(ctx context.Context, level slog.Level) bool {
|
|
return true
|
|
}
|
|
|
|
func (l *TestLogger) WithAttrs(attrs []slog.Attr) slog.Handler {
|
|
return l
|
|
}
|
|
|
|
func (l *TestLogger) WithGroup(name string) slog.Handler {
|
|
return l
|
|
}
|